events/Classes/Service/DestinationDataImportService/DataFetcher.php
Daniel Siepmann 3d6bf0ac8a Add import configuration record
It is now possible to create "Import" records in TYPO3 system.
Those records can then be imported via two new commands.
There is no need to configure everything within the command itself.

That allows:
    * To allow editors to maintain import configuration.
    * Have proper labels, description, csh and UI to create import.
      It no longer is necessary to copy UIDs,
      instead proper wizards from TCA are available to select pages.

Relates: #9649
2022-01-27 15:50:36 +01:00

108 lines
2.7 KiB
PHP

<?php
namespace Wrm\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 Wrm\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,
[]
);
}
}