events/Classes/Service/DestinationDataImportService/DataFetcher.php
Daniel Siepmann a9f3f108e3
BREAKING: Change of vendor/namespace (#43)
The vendor was renamed from `wrm` to `werkraummedia`.
And the namespace vendor was renamed from `Wrm` to `WerkraumMedia`.

That way all references to PHP classes as well as the package name
itself need to be adjusted.
2023-11-09 10:27:43 +01:00

108 lines
2.8 KiB
PHP

<?php
namespace WerkraumMedia\Events\Service\DestinationDataImportService;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Log\Logger;
use TYPO3\CMS\Core\Log\LogManager;
use WerkraumMedia\Events\Domain\Model\Import;
/**
* Provides API to fetch data from remote.
*
* Only partially migrated from service to here.
* Further calls need to be migrated.
*/
class DataFetcher
{
/**
* @var UrlFactory
*/
private $urlFactory;
/**
* @var RequestFactoryInterface
*/
private $requestFactory;
/**
* @var GuzzleClientInterface
*/
private $client;
/**
* @var Logger
*/
private $logger;
public function __construct(
UrlFactory $urlFactory,
LogManager $logManager,
RequestFactoryInterface $requestFactory,
GuzzleClientInterface $client
) {
$this->urlFactory = $urlFactory;
$this->requestFactory = $requestFactory;
$this->client = $client;
$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;
}
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,
[]
);
}
}