<?php declare(strict_types=1); /* * This file is part of alox bundle for Contao. * * (c) Benjamin Roth * * @license commercial */ namespace vossmedien\AloxBundle\API; use Contao\System; use Symfony\Component\Serializer\SerializerInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; use vossmedien\AloxBundle\Model\KatalogModel; use vossmedien\AloxBundle\Model\StellenModel; class Zvoove { /** * @var SerializerInterface */ protected $serializer; /** * @var HttpClientInterface */ protected $httpClient; protected $apiKey; protected $apiDomain; public function __construct(SerializerInterface $serializer, HttpClientInterface $httpClient) { $this->serializer = $serializer; $this->httpClient = $httpClient; $this->apiKey = System::getContainer()->getParameter('vossmedien_alox.zvoove.api_key'); $this->apiDomain = System::getContainer()->getParameter('vossmedien_alox.zvoove.api_domain'); } protected function getApiKey() { return $this->apiKey; } protected function getApiDomain() { return rtrim($this->apiDomain,'/'); } protected function getAuthorizeRequestHeader() { $header = [ 'X-ApiKey' => $this->getApiKey() ]; return $header; } protected function sendRequest(string $relEndpoint, array $options, string $method = 'GET') { $relEndpoint = '/' . ltrim($relEndpoint,'/'); return $this->httpClient->request($method,$this->getApiDomain().$relEndpoint,$options); } public function getStellenFiltered(array $params=[]): ?StellenModel { $options = [ 'headers' => $this->getAuthorizeRequestHeader() ]; if (count($params)) { $options['query'] = $params; } $response = $this->sendRequest('Stelle/GetStellenFiltered',$options); if ($response->getStatusCode() == 200) { $content = $response->getContent(); /** @var StellenModel $collection */ $collection = $this->serializer->deserialize($content,StellenModel::class,'json'); return $collection; } return null; } public function getKatalogByRelationName(string $entityName, string $relationName): ?KatalogModel { $options = [ 'headers' => $this->getAuthorizeRequestHeader(), 'query' => [ 'entityName' => $entityName, 'relationName' => $relationName ] ]; $response = $this->sendRequest('Katalog/GetByRelationName',$options); if ($response->getStatusCode() == 200) { $content = $response->getContent(); /** @var KatalogModel $collection */ $collection = $this->serializer->deserialize($content,KatalogModel::class,'json'); return $collection; } return null; } }