src/Service/CompanyService.php line 75

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