<?phpnamespace App\Service;use App\Entity\Company;use App\Entity\CompanyAddress;use App\Entity\CompanyEndCustomer;use App\Entity\PotentialCustomerCompany;use App\Exception\AccessDeniedException;use App\Repository\CompanyRepository;use App\Security\Authorization\Voter\CompanyVoter;use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;class CompanyService{ /** * @var AuthorizationCheckerInterface */ protected AuthorizationCheckerInterface $authorizationChecker; /** * @var PotentialCustomerCompanyService */ protected PotentialCustomerCompanyService $potentialCustomerCompanyService; /** * @var CompanyRepository */ protected CompanyRepository $repository; /** * @param CompanyRepository $repository * @param AuthorizationCheckerInterface $authorizationChecker * @param PotentialCustomerCompanyService $potentialCustomerCompanyService */ public function __construct( CompanyRepository $repository, AuthorizationCheckerInterface $authorizationChecker, PotentialCustomerCompanyService $potentialCustomerCompanyService ) { $this->repository = $repository; $this->potentialCustomerCompanyService = $potentialCustomerCompanyService; $this->authorizationChecker = $authorizationChecker; } /** * @param Company $company * * @throws AccessDeniedException */ public function assertCreate(Company $company): void { if (!$this->canCreate($company)) { throw new AccessDeniedException('You are not allowed to create a company.'); } } /** * @param Company $company * * @throws AccessDeniedException */ public function assertDelete(Company $company): void { if (!$this->canDelete($company)) { throw new AccessDeniedException('You are not allowed to delete the company.'); } } /** * @param Company $company * * @throws AccessDeniedException */ public function assertRead(Company $company): void { if (!$this->canRead($company)) { throw new AccessDeniedException('You are not allowed to view the company.'); } } /** * @param Company $company * * @throws AccessDeniedException */ public function assertUpdate(Company $company): void { if (!$this->canUpdate($company)) { throw new AccessDeniedException('You are not allowed to update the company.'); } } /** * @param Company $company * * @return bool */ public function canCreate(Company $company): bool { return $this->authorizationChecker->isGranted( CompanyVoter::CREATE, $company ); } /** * @param Company $company * * @return bool */ public function canDelete(Company $company): bool { return $this->authorizationChecker->isGranted( CompanyVoter::DELETE, $company ); } /** * @param Company $company * * @return bool */ public function canRead(Company $company): bool { return $this->authorizationChecker->isGranted( CompanyVoter::READ, $company ); } /** * @param Company $company * * @return bool */ public function canUpdate(Company $company): bool { return $this->authorizationChecker->isGranted( CompanyVoter::UPDATE, $company ); } /** * @param Company $company */ public function create(Company $company): void { $this->repository->save($company); // Add a potential customer company to the sales journal $potentialCustomerCompany = new PotentialCustomerCompany(); $potentialCustomerCompany->setCompany($company); $potentialCustomerCompany->setName($company->getName()); $this->potentialCustomerCompanyService->create($potentialCustomerCompany); } /** * @param CompanyAddress $address */ public function createAddress(CompanyAddress $address): void { $this->repository->save($address); } /** * @param CompanyEndCustomer $customer */ public function createCustomer(CompanyEndCustomer $customer): void { $this->repository->save($customer); } /** * @param Company $company */ public function delete(Company $company): void { $this->repository->delete($company); } /** * @param CompanyAddress $address */ public function deleteAddress(CompanyAddress $address): void { $this->repository->delete($address); } /** * @param CompanyEndCustomer $customer */ public function deleteCustomer(CompanyEndCustomer $customer): void { $this->repository->delete($customer); } /** * @return Company[] */ public function getAll(): array { return $this->repository->findAll(); } /** * @param Company $company */ public function update(Company $company): void { $this->repository->save($company); } /** * @param CompanyAddress $address */ public function updateAddress(CompanyAddress $address): void { $this->repository->save($address); } /** * @param CompanyEndCustomer $customer */ public function updateCustomer(CompanyEndCustomer $customer): void { $this->repository->save($customer); }}