src/Service/SupplierService.php line 76

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Supplier;
  4. use App\Exception\AccessDeniedException;
  5. use App\Repository\SupplierRepository;
  6. use App\Security\Authorization\Voter\SupplierVoter;
  7. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  8. class SupplierService
  9. {
  10.     /**
  11.      * @var AuthorizationCheckerInterface
  12.      */
  13.     protected AuthorizationCheckerInterface $authorizationChecker;
  14.     /**
  15.      * @var SupplierRepository
  16.      */
  17.     protected SupplierRepository $repository;
  18.     /**
  19.      * @param SupplierRepository $repository
  20.      * @param AuthorizationCheckerInterface $authorizationChecker
  21.      */
  22.     public function __construct(
  23.         SupplierRepository $repository,
  24.         AuthorizationCheckerInterface $authorizationChecker
  25.     ) {
  26.         $this->repository $repository;
  27.         $this->authorizationChecker $authorizationChecker;
  28.     }
  29.     /**
  30.      * @param Supplier $supplier
  31.      *
  32.      * @throws AccessDeniedException
  33.      */
  34.     public function assertCreate(Supplier $supplier): void
  35.     {
  36.         if (!$this->canCreate($supplier)) {
  37.             throw new AccessDeniedException('You are not allowed to create a supplier.');
  38.         }
  39.     }
  40.     /**
  41.      * @param Supplier $supplier
  42.      *
  43.      * @throws AccessDeniedException
  44.      */
  45.     public function assertDelete(Supplier $supplier): void
  46.     {
  47.         if (!$this->canDelete($supplier)) {
  48.             throw new AccessDeniedException('You are not allowed to delete the supplier.');
  49.         }
  50.     }
  51.     /**
  52.      * @param Supplier $supplier
  53.      *
  54.      * @throws AccessDeniedException
  55.      */
  56.     public function assertRead(Supplier $supplier): void
  57.     {
  58.         if (!$this->canRead($supplier)) {
  59.             throw new AccessDeniedException('You are not allowed to view the supplier.');
  60.         }
  61.     }
  62.     /**
  63.      * @param Supplier $supplier
  64.      *
  65.      * @throws AccessDeniedException
  66.      */
  67.     public function assertUpdate(Supplier $supplier): void
  68.     {
  69.         if (!$this->canUpdate($supplier)) {
  70.             throw new AccessDeniedException('You are not allowed to update the supplier.');
  71.         }
  72.     }
  73.     /**
  74.      * @param Supplier $supplier
  75.      *
  76.      * @return bool
  77.      */
  78.     public function canCreate(Supplier $supplier): bool
  79.     {
  80.         return $this->authorizationChecker->isGranted(
  81.             SupplierVoter::CREATE,
  82.             $supplier
  83.         );
  84.     }
  85.     /**
  86.      * @param Supplier $supplier
  87.      *
  88.      * @return bool
  89.      */
  90.     public function canDelete(Supplier $supplier): bool
  91.     {
  92.         return $this->authorizationChecker->isGranted(
  93.             SupplierVoter::DELETE,
  94.             $supplier
  95.         );
  96.     }
  97.     /**
  98.      * @param Supplier $supplier
  99.      *
  100.      * @return bool
  101.      */
  102.     public function canRead(Supplier $supplier): bool
  103.     {
  104.         return $this->authorizationChecker->isGranted(
  105.             SupplierVoter::READ,
  106.             $supplier
  107.         );
  108.     }
  109.     /**
  110.      * @param Supplier $supplier
  111.      *
  112.      * @return bool
  113.      */
  114.     public function canUpdate(Supplier $supplier): bool
  115.     {
  116.         return $this->authorizationChecker->isGranted(
  117.             SupplierVoter::UPDATE,
  118.             $supplier
  119.         );
  120.     }
  121.     /**
  122.      * @param Supplier $supplier
  123.      */
  124.     public function create(Supplier $supplier): void
  125.     {
  126.         $inventory null;
  127.         if ($supplier->isInventoryAddress()) {
  128.             $inventory $this->getInventory();
  129.         }
  130.         $this->repository->save($supplier);
  131.         if (null !== $inventory && $inventory !== $supplier && $inventory->isInventoryAddress()) {
  132.             $inventory->setInventoryAddress(false);
  133.             $this->repository->save($inventory);
  134.         }
  135.     }
  136.     /**
  137.      * @param Supplier $supplier
  138.      */
  139.     public function delete(Supplier $supplier): void
  140.     {
  141.         $this->repository->delete($supplier);
  142.     }
  143.     /**
  144.      * @return array<Supplier>
  145.      */
  146.     public function getAll(): array
  147.     {
  148.         return $this->repository->findBy(
  149.             [],
  150.             [
  151.                 'name' => 'ASC',
  152.             ]
  153.         );
  154.     }
  155.     /**
  156.      * @param string $label
  157.      *
  158.      * @return Supplier|null
  159.      */
  160.     public function getByLabel(string $label): ?Supplier
  161.     {
  162.         return $this->repository->findByLabel($label);
  163.     }
  164.     /**
  165.      * @return Supplier|null
  166.      */
  167.     public function getInventory(): ?Supplier
  168.     {
  169.         return $this->repository->findInventory();
  170.     }
  171.     /**
  172.      * @return SupplierRepository
  173.      */
  174.     public function getRepository(): SupplierRepository
  175.     {
  176.         return $this->repository;
  177.     }
  178.     /**
  179.      * @param Supplier $supplier
  180.      */
  181.     public function update(Supplier $supplier): void
  182.     {
  183.         $inventory null;
  184.         if ($supplier->isInventoryAddress()) {
  185.             $inventory $this->getInventory();
  186.         }
  187.         $this->repository->save($supplier);
  188.         if (null !== $inventory && $inventory !== $supplier && $inventory->isInventoryAddress()) {
  189.             $inventory->setInventoryAddress(false);
  190.             $this->repository->save($inventory);
  191.         }
  192.     }
  193. }