<?php
namespace App\Controller\Frontend;
use App\Constants\ContractConstants;
use App\Entity\Customer;
use App\Entity\Sponsor;
use App\Service\Utils\CustomerServiceInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class SponsorController extends AbstractController
{
private ?Customer $customer;
private EntityManagerInterface $entityManager;
/**
* @param EntityManagerInterface $entityManager
* @param CustomerServiceInterface $customerService
*/
public function __construct(
EntityManagerInterface $entityManager,
CustomerServiceInterface $customerService
) {
$this->customer = $customerService->getCustomerByHost();
$this->entityManager = $entityManager;
}
/**
* @return Response
*/
#[Route('/sponsor/liste', name: 'app_frontend_sponsor_list')]
public function donatorList(): Response
{
$sponsors = [];
$sponsorCollection = $this->entityManager->getRepository(Sponsor::class)
->findBy(
['customer' => $this->customer->getId()],
['id' => 'DESC']
);
foreach ($sponsorCollection as $sponsor) {
foreach ($sponsor->getContracts() as $contract) {
if (
($contract->getState() === ContractConstants::WORKFLOW_PLACE_ACTIVE ||
$contract->getState() === ContractConstants::WORKFLOW_PLACE_CANCELLED) &&
!$this->isSponsorInList($sponsors, $sponsor)
) {
$sponsors[] = $sponsor;
}
}
}
return $this->render('frontend/sponsor/list.html.twig', [
'sponsors' => $sponsors,
]);
}
/**
* @param Sponsor[] $list
* @param Sponsor $sponsor
*
* @return bool
*/
protected function isSponsorInList(array $list, Sponsor $sponsor): bool
{
foreach ($list as $item) {
if ($item->getId() === $sponsor->getId()) {
return true;
}
}
return false;
}
}