src/Controller/Frontend/SponsorController.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Constants\ContractConstants;
  4. use App\Entity\Customer;
  5. use App\Entity\Sponsor;
  6. use App\Service\Utils\CustomerServiceInterface;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class SponsorController extends AbstractController
  12. {
  13.     private ?Customer $customer;
  14.     private EntityManagerInterface $entityManager;
  15.     /**
  16.      * @param EntityManagerInterface $entityManager
  17.      * @param CustomerServiceInterface $customerService
  18.      */
  19.     public function __construct(
  20.         EntityManagerInterface $entityManager,
  21.         CustomerServiceInterface $customerService
  22.     ) {
  23.         $this->customer $customerService->getCustomerByHost();
  24.         $this->entityManager $entityManager;
  25.     }
  26.     /**
  27.      * @return Response
  28.      */
  29.     #[Route('/sponsor/liste'name'app_frontend_sponsor_list')]
  30.     public function donatorList(): Response
  31.     {
  32.         $sponsors = [];
  33.         $sponsorCollection $this->entityManager->getRepository(Sponsor::class)
  34.             ->findBy(
  35.                 ['customer' => $this->customer->getId()],
  36.                 ['id' => 'DESC']
  37.             );
  38.         foreach ($sponsorCollection as $sponsor) {
  39.             foreach ($sponsor->getContracts() as $contract) {
  40.                 if (
  41.                     ($contract->getState() === ContractConstants::WORKFLOW_PLACE_ACTIVE ||
  42.                     $contract->getState() === ContractConstants::WORKFLOW_PLACE_CANCELLED) &&
  43.                     !$this->isSponsorInList($sponsors$sponsor)
  44.                 ) {
  45.                     $sponsors[] = $sponsor;
  46.                 }
  47.             }
  48.         }
  49.         return $this->render('frontend/sponsor/list.html.twig', [
  50.             'sponsors' => $sponsors,
  51.         ]);
  52.     }
  53.     /**
  54.      * @param Sponsor[] $list
  55.      * @param Sponsor $sponsor
  56.      *
  57.      * @return bool
  58.      */
  59.     protected function isSponsorInList(array $listSponsor $sponsor): bool
  60.     {
  61.         foreach ($list as $item) {
  62.             if ($item->getId() === $sponsor->getId()) {
  63.                 return true;
  64.             }
  65.         }
  66.         return false;
  67.     }
  68. }