src/Aqarmap/Bundle/FinancialAidsBundle/Service/FinancialAidService.php line 117

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\FinancialAidsBundle\Service;
  3. use Aqarmap\Bundle\FinancialAidsBundle\Constant\FinancialAidConstraints;
  4. use Aqarmap\Bundle\FinancialAidsBundle\Entity\FinancialAid;
  5. use Aqarmap\Bundle\FinancialAidsBundle\Utilities\FinancialAidURL;
  6. use Aqarmap\Bundle\ListingBundle\Constant\CountryCodes;
  7. use Aqarmap\Bundle\ListingBundle\Entity\Listing;
  8. use Aqarmap\Bundle\MainBundle\Service\Setting;
  9. use Doctrine\ORM\EntityManager;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Doctrine\ORM\Query;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Component\Routing\RouterInterface;
  16. class FinancialAidService
  17. {
  18.     public const PAGE 1;
  19.     public const PAGE_LIMIT 10;
  20.     /**
  21.      * @var EntityManagerInterface
  22.      */
  23.     private $entityManager;
  24.     /**
  25.      * @var PaginatorInterface
  26.      */
  27.     private $paginator;
  28.     /**
  29.      * @var RouterInterface
  30.      */
  31.     private $router;
  32.     /**
  33.      * @var Setting
  34.      */
  35.     private $settingService;
  36.     /**
  37.      * @param EntityManagerInterface EntityManager
  38.      * @param PaginatorInterface PaginatorInterface
  39.      */
  40.     public function __construct(EntityManagerInterface $entityManagerPaginatorInterface $paginatorRouterInterface $routerSetting $settingService)
  41.     {
  42.         $this->entityManager $entityManager;
  43.         $this->paginator $paginator;
  44.         $this->router $router;
  45.         $this->settingService $settingService;
  46.     }
  47.     /**
  48.      * @return Query
  49.      */
  50.     public function getFinancialAids()
  51.     {
  52.         return $this->getRepository()->getFinancialAids();
  53.     }
  54.     /**
  55.      * @param FinancialAid
  56.      *
  57.      * @return \Knp\Component\Pager\Pagination\PaginationInterface
  58.      */
  59.     public function paginate($financialAids$paginationPageNumberAndLimit)
  60.     {
  61.         return $this->paginator->paginate($financialAids$paginationPageNumberAndLimit['pageNumber'], $paginationPageNumberAndLimit['pageLimit']);
  62.     }
  63.     /**
  64.      * @param Request
  65.      *
  66.      * @return array $paginationPageAndNumber
  67.      */
  68.     public function getPaginationPageNumberAndLimit($request)
  69.     {
  70.         return [
  71.             'pageNumber' => $request->query->get('page'self::PAGE),
  72.             'pageLimit' => $request->query->get('limit'self::PAGE_LIMIT),
  73.         ];
  74.     }
  75.     /**
  76.      * @param FinancialAid
  77.      */
  78.     public function saveFinancialAid($financialAid): void
  79.     {
  80.         $this->entityManager->persist($financialAid);
  81.         $this->entityManager->flush();
  82.     }
  83.     /**
  84.      * @param FinancialAid
  85.      */
  86.     public function flushData($financialAid): void
  87.     {
  88.         $this->entityManager->flush($financialAid);
  89.     }
  90.     /**
  91.      * @param FinancialAid
  92.      * @param int $status
  93.      */
  94.     public function updateFinancialAidStatus($financialAid$status): void
  95.     {
  96.         $financialAid->setStatus($status);
  97.         $this->saveFinancialAid($financialAid);
  98.     }
  99.     /**
  100.      * @throws \Doctrine\ORM\NonUniqueResultException
  101.      */
  102.     public function setFinancialAidInListing(Listing $listing): void
  103.     {
  104.         if (\in_array($listing->getSection()->getId(), FinancialAidConstraints::ALLOWED_LISTING_SECTIONS_TO_ADD_FINANCE_URL)) {
  105.             $financialAid $this->getRepository()->getRandomFinancialAid();
  106.             if ($financialAid && $financialAid->getBankUrl()) {
  107.                 $listing->setFinancialAid($financialAid);
  108.             }
  109.         }
  110.     }
  111.     /**
  112.      * @throws \Doctrine\ORM\NonUniqueResultException
  113.      */
  114.     public function getMortgageRequestLink(): string
  115.     {
  116.         $link $this->router->generate('mortgage_request', [], UrlGeneratorInterface::ABSOLUTE_URL);
  117.         if ($this->isEgyptSite()) {
  118.             $bank $this->getRepository()->getRandomFinancialAid();
  119.             if ($bank) {
  120.                 $link FinancialAidURL::getUrl($bank);
  121.             }
  122.         }
  123.         return $link;
  124.     }
  125.     /**
  126.      * @return \Aqarmap\Bundle\FinancialAidsBundle\Repository\FinancialAidRepository
  127.      */
  128.     protected function getRepository()
  129.     {
  130.         return $this->entityManager->getRepository('AqarmapFinancialAidsBundle:FinancialAid');
  131.     }
  132.     private function isEgyptSite(): bool
  133.     {
  134.         return CountryCodes::EGYPT == $this->settingService->getSetting('general''country');
  135.     }
  136. }