<?php
namespace App\Repository;
use App\Infrastructure\Service\Impl\RefreshJwtTokenServiceImpl;
use App\Infrastructure\Service\Impl\VerifyTokenServiceImpl;
use App\Repository\ApiPlatform\CollectionPigeResponse;
use App\Repository\ApiPlatform\CollectionResponse;
use App\Repository\ApiPlatform\Response;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use JsonMapper\JsonMapperFactory;
use JsonMapper\JsonMapperInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Helper\MonthLabelConverterHelper;
use App\Infrastructure\Traits\HydraTrait;
abstract class AbstractRepository
{
use HydraTrait;
/** @var string */
protected string $milkiyaBaseUrl;
protected LoggerInterface $logger;
protected Security $security;
protected JsonMapperInterface $mapper;
protected SessionInterface $session;
protected RequestStack $requestStack;
protected TranslatorInterface $translator;
protected MonthLabelConverterHelper $monthLabelConverterHelper;
protected ClientInterface $client;
protected SerializerInterface $serializer;
protected CacheInterface $cache;
protected VerifyTokenServiceImpl $verifyAuthToken;
protected RefreshJwtTokenServiceImpl $refreshAuthToken;
/**
* @param string $milkiyaBaseUrl
* @param LoggerInterface $logger
* @param Security $security
* @param RequestStack $requestStack
* @param TranslatorInterface $translator
* @param MonthLabelConverterHelper $monthLabelConverterHelper
* @param SerializerInterface $serializer
* @param CacheInterface $cache
* @param VerifyTokenServiceImpl $verifyAuthToken
* @param RefreshJwtTokenServiceImpl $refreshAuthToken
*/
public function __construct(
string $milkiyaBaseUrl,
LoggerInterface $logger,
Security $security,
RequestStack $requestStack,
TranslatorInterface $translator,
MonthLabelConverterHelper $monthLabelConverterHelper,
SerializerInterface $serializer,
CacheInterface $cache,
VerifyTokenServiceImpl $verifyAuthToken,
RefreshJwtTokenServiceImpl $refreshAuthToken,
ClientInterface $client = null
) {
$this->milkiyaBaseUrl = $milkiyaBaseUrl;
$this->logger = $logger;
$this->security = $security;
$this->session = $requestStack->getSession();
$this->mapper = (new JsonMapperFactory())->bestFit();
$this->requestStack = $requestStack;
$this->translator = $translator;
$this->monthLabelConverterHelper = $monthLabelConverterHelper;
$this->serializer = $serializer;
$this->cache = $cache;
$this->verifyAuthToken = $verifyAuthToken;
$this->refreshAuthToken = $refreshAuthToken;
if ($client === null) {
$this->setUpClient();
} else {
$this->client = $client;
}
}
/**
* set up default parameters
*
* @return void
*/
private function setUpClient(): void
{
$user = $this->security->getUser();
$this->client = new Client([
'headers' => [
'Content-Type' => 'application/json',
'Accept-Language' => $this->translator->getLocale(),
'Authorization' => "Bearer " . ($user instanceof UserInterface ? $user->getToken() : null)
],
'verify' => false,
'base_uri' => $this->milkiyaBaseUrl,
]);
}
/**
* Retrieve all data
*
* @return CollectionResponse|null
*/
abstract public function findAll();
/**
* Retrieve selected data
*
* @param array $filters
*
* @return CollectionResponse|CollectionPigeResponse|null
*/
abstract public function findBy(array $filters);
/**
* @param string $id
*
* @return Response|null
*/
abstract public function find(string $id): ?Response;
/**
* @param array $filters
*
* @return Response|null
*/
abstract public function findOneBy(array $filters): ?Response;
/**
* @param object $data
*
* @return bool
*/
abstract public function persist(object $data): bool;
/**
* @param int $id
* @param object $data
*
* @return bool
*/
abstract public function update(int $id, object $data): bool;
}