<?php
namespace App\Controller;
use App\Entity\Notifications;
use App\Entity\Permission;
use App\Entity\Product;
use App\Entity\Stock;
use App\Entity\Synchro;
use App\Entity\User;
use App\Repository\ProductRepository;
use App\Repository\StockRepository;
use Doctrine\ORM\EntityManagerInterface;
use phpDocumentor\Reflection\Types\Boolean;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use function PHPUnit\Framework\throwException;
class AppController extends AbstractController
{
private $em;
public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
}
/**
* @Route("/", name="home")
*/
public function index(): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
return $this->redirectToRoute('app_checkout', []);
}
public function renderWithParams(string $view, array $params): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
if($this->getUser() != null){
/** @var User $user */
$user = $this->em->getRepository(User::class)->findOneBy(['username' => $this->getUser()->getUserIdentifier()]);
}else{
/** @var User $user */
$user = $this->em->getRepository(User::class)->find(5);
}
$notifications = $this->getNotifications();
$synch = $this->lastSynchro();
$shops = $user->getUserShops()->toArray();
$currentShop = $user->getCurrentshop() ? $user->getCurrentshop() : $shops[0];
$rended_params = array_merge([
'user' => $user,
'permissions' => $this->getPermissions($user->getProfile()->getProfilePermissions()->toArray()),
'profile' => $user->getUserIdentifier(),
'userShops' => $shops,
'currentShop' => $currentShop,
'lastSynch' => $synch['lastSynch'],
'nextSynch' => $synch['nextSynch'],
'notifications' => $notifications
], $params);
return $this->render($view, $rended_params);
}
public function grantAccess(string $acess, User $user): Boolean
{
$permissions = $user->getProfile()->getProfilePermissions();
foreach ($permissions as $permission) if($permission == $acess){
return true;
}
return false;
}
/**
* @param array $profilePermissions
* @return array
*/
private function getPermissions(array $profilePermissions){
$permissions = [];
/** @var Permission $permission */
foreach ($profilePermissions as $permission){
$permissions[] = $permission->getLabel();
}
return $permissions;
}
/**
* @return array
*/
private function getAlerts(){
$alerts = [];
$seuil = [];
$expiration = [];
$seuil = $this->em->getRepository(Stock::class)->findAll();
$expiration = $this->em->getRepository(Stock::class)->findAll();
$alerts = array_merge($alerts, $seuil);
$alerts = array_merge($alerts, $expiration);
return $alerts;
}
private function lastSynchro(){
/** @var Synchro $synch */
$synch = $this->em->getRepository(Synchro::class)->getLastSynchro()[0];
$synch = $this->em->getRepository(Synchro::class)->getLastSynchro()[0];
$timestamp = strtotime($synch->getMoment());
/** @var \DateTime $lastSynchDate */
$nextSynchDate = (new \DateTime())->setTimestamp($timestamp) ;
$lastSynchDate = clone ($nextSynchDate);
$nextSynchDate->add(new \DateInterval('PT1H'));
return ['lastSynch' => $lastSynchDate->format('d/m/y H:i') , 'nextSynch' => $nextSynchDate->format('d/m/y H:i')];
}
private function getNotifications(): array
{
$notificationRepository = $this->em->getRepository(Notifications::class);
$notifications['EXP'] = $notificationRepository->findBy(['type' => 'EXPIRATION']);
$notifications['LIMIT'] = $notificationRepository->findBy(['type' => 'LIMIT']);
return $notifications ;
}
}