<?phpnamespace App\Service;use App\Entity\Supplier;use App\Exception\AccessDeniedException;use App\Repository\SupplierRepository;use App\Security\Authorization\Voter\SupplierVoter;use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;class SupplierService{ /** * @var AuthorizationCheckerInterface */ protected AuthorizationCheckerInterface $authorizationChecker; /** * @var SupplierRepository */ protected SupplierRepository $repository; /** * @param SupplierRepository $repository * @param AuthorizationCheckerInterface $authorizationChecker */ public function __construct( SupplierRepository $repository, AuthorizationCheckerInterface $authorizationChecker ) { $this->repository = $repository; $this->authorizationChecker = $authorizationChecker; } /** * @param Supplier $supplier * * @throws AccessDeniedException */ public function assertCreate(Supplier $supplier): void { if (!$this->canCreate($supplier)) { throw new AccessDeniedException('You are not allowed to create a supplier.'); } } /** * @param Supplier $supplier * * @throws AccessDeniedException */ public function assertDelete(Supplier $supplier): void { if (!$this->canDelete($supplier)) { throw new AccessDeniedException('You are not allowed to delete the supplier.'); } } /** * @param Supplier $supplier * * @throws AccessDeniedException */ public function assertRead(Supplier $supplier): void { if (!$this->canRead($supplier)) { throw new AccessDeniedException('You are not allowed to view the supplier.'); } } /** * @param Supplier $supplier * * @throws AccessDeniedException */ public function assertUpdate(Supplier $supplier): void { if (!$this->canUpdate($supplier)) { throw new AccessDeniedException('You are not allowed to update the supplier.'); } } /** * @param Supplier $supplier * * @return bool */ public function canCreate(Supplier $supplier): bool { return $this->authorizationChecker->isGranted( SupplierVoter::CREATE, $supplier ); } /** * @param Supplier $supplier * * @return bool */ public function canDelete(Supplier $supplier): bool { return $this->authorizationChecker->isGranted( SupplierVoter::DELETE, $supplier ); } /** * @param Supplier $supplier * * @return bool */ public function canRead(Supplier $supplier): bool { return $this->authorizationChecker->isGranted( SupplierVoter::READ, $supplier ); } /** * @param Supplier $supplier * * @return bool */ public function canUpdate(Supplier $supplier): bool { return $this->authorizationChecker->isGranted( SupplierVoter::UPDATE, $supplier ); } /** * @param Supplier $supplier */ public function create(Supplier $supplier): void { $inventory = null; if ($supplier->isInventoryAddress()) { $inventory = $this->getInventory(); } $this->repository->save($supplier); if (null !== $inventory && $inventory !== $supplier && $inventory->isInventoryAddress()) { $inventory->setInventoryAddress(false); $this->repository->save($inventory); } } /** * @param Supplier $supplier */ public function delete(Supplier $supplier): void { $this->repository->delete($supplier); } /** * @return array<Supplier> */ public function getAll(): array { return $this->repository->findBy( [], [ 'name' => 'ASC', ] ); } /** * @param string $label * * @return Supplier|null */ public function getByLabel(string $label): ?Supplier { return $this->repository->findByLabel($label); } /** * @return Supplier|null */ public function getInventory(): ?Supplier { return $this->repository->findInventory(); } /** * @return SupplierRepository */ public function getRepository(): SupplierRepository { return $this->repository; } /** * @param Supplier $supplier */ public function update(Supplier $supplier): void { $inventory = null; if ($supplier->isInventoryAddress()) { $inventory = $this->getInventory(); } $this->repository->save($supplier); if (null !== $inventory && $inventory !== $supplier && $inventory->isInventoryAddress()) { $inventory->setInventoryAddress(false); $this->repository->save($inventory); } }}