2021-12-15 12:28:06 +01:00
|
|
|
<?php
|
|
|
|
|
2023-11-09 10:27:43 +01:00
|
|
|
namespace WerkraumMedia\Events\Service\DestinationDataImportService;
|
2021-12-15 12:28:06 +01:00
|
|
|
|
|
|
|
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
|
|
|
|
use Psr\Http\Client\ClientInterface;
|
|
|
|
use Psr\Http\Message\RequestFactoryInterface;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2022-01-26 14:16:12 +01:00
|
|
|
use TYPO3\CMS\Core\Log\Logger;
|
|
|
|
use TYPO3\CMS\Core\Log\LogManager;
|
2023-11-09 10:27:43 +01:00
|
|
|
use WerkraumMedia\Events\Domain\Model\Import;
|
2021-12-15 12:28:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides API to fetch data from remote.
|
|
|
|
*
|
|
|
|
* Only partially migrated from service to here.
|
|
|
|
* Further calls need to be migrated.
|
|
|
|
*/
|
|
|
|
class DataFetcher
|
|
|
|
{
|
2022-01-26 14:16:12 +01:00
|
|
|
/**
|
|
|
|
* @var UrlFactory
|
|
|
|
*/
|
|
|
|
private $urlFactory;
|
|
|
|
|
2021-12-15 12:28:06 +01:00
|
|
|
/**
|
|
|
|
* @var RequestFactoryInterface
|
|
|
|
*/
|
|
|
|
private $requestFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var GuzzleClientInterface
|
|
|
|
*/
|
|
|
|
private $client;
|
|
|
|
|
2022-01-26 14:16:12 +01:00
|
|
|
/**
|
|
|
|
* @var Logger
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
2021-12-15 12:28:06 +01:00
|
|
|
public function __construct(
|
2022-01-26 14:16:12 +01:00
|
|
|
UrlFactory $urlFactory,
|
|
|
|
LogManager $logManager,
|
2021-12-15 12:28:06 +01:00
|
|
|
RequestFactoryInterface $requestFactory,
|
|
|
|
GuzzleClientInterface $client
|
|
|
|
) {
|
2022-01-26 14:16:12 +01:00
|
|
|
$this->urlFactory = $urlFactory;
|
2021-12-15 12:28:06 +01:00
|
|
|
$this->requestFactory = $requestFactory;
|
|
|
|
$this->client = $client;
|
2022-01-26 14:16:12 +01:00
|
|
|
|
|
|
|
$this->logger = $logManager->getLogger(__CLASS__);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetchSearchResult(Import $import): array
|
|
|
|
{
|
|
|
|
$url = $this->urlFactory->createSearchResultUrl($import);
|
|
|
|
|
|
|
|
$this->logger->info('Try to get data from ' . $url);
|
|
|
|
|
|
|
|
if ($this->client instanceof ClientInterface) {
|
|
|
|
// Keep after TYPO3 10 was dropped
|
|
|
|
$response = $this->client->sendRequest(
|
|
|
|
$this->requestFactory->createRequest(
|
|
|
|
'GET',
|
|
|
|
$url
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Drop once TYPO3 10 support was dropped
|
|
|
|
$response = $this->client->request(
|
|
|
|
'GET',
|
|
|
|
$url,
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$jsonContent = $response->getBody()->__toString();
|
|
|
|
|
|
|
|
$jsonResponse = json_decode($jsonContent, true);
|
|
|
|
if (is_array($jsonResponse) === false) {
|
|
|
|
throw new \Exception('No valid JSON fetched, got: "' . $jsonContent . '".', 1639495835);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->logger->info('Received data with ' . count($jsonResponse['items']) . ' items');
|
|
|
|
|
|
|
|
return $jsonResponse;
|
2021-12-15 12:28:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function fetchImage(string $url): ResponseInterface
|
|
|
|
{
|
|
|
|
// Keep after TYPO3 10 was dropped
|
|
|
|
if ($this->client instanceof ClientInterface) {
|
|
|
|
return $this->client->sendRequest(
|
|
|
|
$this->requestFactory->createRequest(
|
|
|
|
'GET',
|
|
|
|
$url
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop once TYPO3 10 support was dropped
|
|
|
|
return $this->client->request(
|
|
|
|
'GET',
|
|
|
|
$url,
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|