src/Repository/AbstractRepository.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Infrastructure\Service\Impl\RefreshJwtTokenServiceImpl;
  4. use App\Infrastructure\Service\Impl\VerifyTokenServiceImpl;
  5. use App\Repository\ApiPlatform\CollectionPigeResponse;
  6. use App\Repository\ApiPlatform\CollectionResponse;
  7. use App\Repository\ApiPlatform\Response;
  8. use GuzzleHttp\Client;
  9. use GuzzleHttp\ClientInterface;
  10. use JsonMapper\JsonMapperFactory;
  11. use JsonMapper\JsonMapperInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Component\Security\Core\User\UserInterface;
  17. use Symfony\Component\Serializer\SerializerInterface;
  18. use Symfony\Contracts\Cache\CacheInterface;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. use App\Helper\MonthLabelConverterHelper;
  21. use App\Infrastructure\Traits\HydraTrait;
  22. abstract class AbstractRepository
  23. {
  24.     use HydraTrait;
  25.     /** @var string  */
  26.     protected string $milkiyaBaseUrl;
  27.     protected LoggerInterface $logger;
  28.     protected Security $security;
  29.     protected JsonMapperInterface $mapper;
  30.     protected SessionInterface $session;
  31.     protected RequestStack $requestStack;
  32.     protected TranslatorInterface $translator;
  33.     protected MonthLabelConverterHelper $monthLabelConverterHelper;
  34.     protected ClientInterface $client;
  35.     protected SerializerInterface $serializer;
  36.     protected CacheInterface $cache;
  37.     protected VerifyTokenServiceImpl $verifyAuthToken;
  38.     protected RefreshJwtTokenServiceImpl $refreshAuthToken;
  39.     /**
  40.      * @param string                     $milkiyaBaseUrl
  41.      * @param LoggerInterface            $logger
  42.      * @param Security                   $security
  43.      * @param RequestStack               $requestStack
  44.      * @param TranslatorInterface        $translator
  45.      * @param MonthLabelConverterHelper  $monthLabelConverterHelper
  46.      * @param SerializerInterface        $serializer
  47.      * @param CacheInterface             $cache
  48.      * @param VerifyTokenServiceImpl     $verifyAuthToken
  49.      * @param RefreshJwtTokenServiceImpl $refreshAuthToken
  50.      */
  51.     public function __construct(
  52.         string $milkiyaBaseUrl,
  53.         LoggerInterface $logger,
  54.         Security $security,
  55.         RequestStack $requestStack,
  56.         TranslatorInterface $translator,
  57.         MonthLabelConverterHelper $monthLabelConverterHelper,
  58.         SerializerInterface $serializer,
  59.         CacheInterface $cache,
  60.         VerifyTokenServiceImpl $verifyAuthToken,
  61.         RefreshJwtTokenServiceImpl $refreshAuthToken,
  62.         ClientInterface $client null
  63.     ) {
  64.         $this->milkiyaBaseUrl $milkiyaBaseUrl;
  65.         $this->logger $logger;
  66.         $this->security $security;
  67.         $this->session $requestStack->getSession();
  68.         $this->mapper = (new JsonMapperFactory())->bestFit();
  69.         $this->requestStack $requestStack;
  70.         $this->translator $translator;
  71.         $this->monthLabelConverterHelper $monthLabelConverterHelper;
  72.         $this->serializer $serializer;
  73.         $this->cache $cache;
  74.         $this->verifyAuthToken $verifyAuthToken;
  75.         $this->refreshAuthToken $refreshAuthToken;
  76.         if ($client === null) {
  77.             $this->setUpClient();
  78.         } else {
  79.             $this->client $client;
  80.         }
  81.     }
  82.     /**
  83.      * set up default parameters
  84.      *
  85.      * @return void
  86.      */
  87.     private function setUpClient(): void
  88.     {
  89.         $user $this->security->getUser();
  90.         $this->client = new Client([
  91.             'headers' => [
  92.                 'Content-Type' => 'application/json',
  93.                 'Accept-Language' => $this->translator->getLocale(),
  94.                 'Authorization' => "Bearer " . ($user instanceof UserInterface $user->getToken() : null)
  95.             ],
  96.             'verify' => false,
  97.             'base_uri' => $this->milkiyaBaseUrl,
  98.         ]);
  99.     }
  100.     /**
  101.      * Retrieve all data
  102.      *
  103.      * @return CollectionResponse|null
  104.      */
  105.     abstract public function findAll();
  106.     /**
  107.      * Retrieve selected data
  108.      *
  109.      * @param array $filters
  110.      *
  111.      * @return CollectionResponse|CollectionPigeResponse|null
  112.      */
  113.     abstract public function findBy(array $filters);
  114.     /**
  115.      * @param string $id
  116.      *
  117.      * @return Response|null
  118.      */
  119.     abstract public function find(string $id): ?Response;
  120.     /**
  121.      * @param array $filters
  122.      *
  123.      * @return Response|null
  124.      */
  125.     abstract public function findOneBy(array $filters): ?Response;
  126.     /**
  127.      * @param object $data
  128.      *
  129.      * @return bool
  130.      */
  131.     abstract public function persist(object $data): bool;
  132.     /**
  133.      * @param int    $id
  134.      * @param object $data
  135.      *
  136.      * @return bool
  137.      */
  138.     abstract public function update(int $idobject $data): bool;
  139. }