<?php
namespace Aqarmap\Bundle\FinancialAidsBundle\Service;
use Aqarmap\Bundle\FinancialAidsBundle\Constant\FinancialAidConstraints;
use Aqarmap\Bundle\FinancialAidsBundle\Entity\FinancialAid;
use Aqarmap\Bundle\FinancialAidsBundle\Utilities\FinancialAidURL;
use Aqarmap\Bundle\ListingBundle\Constant\CountryCodes;
use Aqarmap\Bundle\ListingBundle\Entity\Listing;
use Aqarmap\Bundle\MainBundle\Service\Setting;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
class FinancialAidService
{
public const PAGE = 1;
public const PAGE_LIMIT = 10;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var PaginatorInterface
*/
private $paginator;
/**
* @var RouterInterface
*/
private $router;
/**
* @var Setting
*/
private $settingService;
/**
* @param EntityManagerInterface EntityManager
* @param PaginatorInterface PaginatorInterface
*/
public function __construct(EntityManagerInterface $entityManager, PaginatorInterface $paginator, RouterInterface $router, Setting $settingService)
{
$this->entityManager = $entityManager;
$this->paginator = $paginator;
$this->router = $router;
$this->settingService = $settingService;
}
/**
* @return Query
*/
public function getFinancialAids()
{
return $this->getRepository()->getFinancialAids();
}
/**
* @param FinancialAid
*
* @return \Knp\Component\Pager\Pagination\PaginationInterface
*/
public function paginate($financialAids, $paginationPageNumberAndLimit)
{
return $this->paginator->paginate($financialAids, $paginationPageNumberAndLimit['pageNumber'], $paginationPageNumberAndLimit['pageLimit']);
}
/**
* @param Request
*
* @return array $paginationPageAndNumber
*/
public function getPaginationPageNumberAndLimit($request)
{
return [
'pageNumber' => $request->query->get('page', self::PAGE),
'pageLimit' => $request->query->get('limit', self::PAGE_LIMIT),
];
}
/**
* @param FinancialAid
*/
public function saveFinancialAid($financialAid): void
{
$this->entityManager->persist($financialAid);
$this->entityManager->flush();
}
/**
* @param FinancialAid
*/
public function flushData($financialAid): void
{
$this->entityManager->flush($financialAid);
}
/**
* @param FinancialAid
* @param int $status
*/
public function updateFinancialAidStatus($financialAid, $status): void
{
$financialAid->setStatus($status);
$this->saveFinancialAid($financialAid);
}
/**
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function setFinancialAidInListing(Listing $listing): void
{
if (\in_array($listing->getSection()->getId(), FinancialAidConstraints::ALLOWED_LISTING_SECTIONS_TO_ADD_FINANCE_URL)) {
$financialAid = $this->getRepository()->getRandomFinancialAid();
if ($financialAid && $financialAid->getBankUrl()) {
$listing->setFinancialAid($financialAid);
}
}
}
/**
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function getMortgageRequestLink(): string
{
$link = $this->router->generate('mortgage_request', [], UrlGeneratorInterface::ABSOLUTE_URL);
if ($this->isEgyptSite()) {
$bank = $this->getRepository()->getRandomFinancialAid();
if ($bank) {
$link = FinancialAidURL::getUrl($bank);
}
}
return $link;
}
/**
* @return \Aqarmap\Bundle\FinancialAidsBundle\Repository\FinancialAidRepository
*/
protected function getRepository()
{
return $this->entityManager->getRepository('AqarmapFinancialAidsBundle:FinancialAid');
}
private function isEgyptSite(): bool
{
return CountryCodes::EGYPT == $this->settingService->getSetting('general', 'country');
}
}