2021-02-01 14:14:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 Daniel Siepmann <coding@daniel-siepmann.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2021-08-10 09:38:59 +02:00
|
|
|
namespace WerkraumMedia\ThueCat\Domain\Import\Importer;
|
|
|
|
|
2021-02-01 14:14:05 +01:00
|
|
|
use Psr\Http\Client\ClientInterface;
|
|
|
|
use Psr\Http\Message\RequestFactoryInterface;
|
2021-05-31 13:32:58 +02:00
|
|
|
use Psr\Http\Message\RequestInterface;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2021-02-16 15:08:15 +01:00
|
|
|
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface as CacheFrontendInterface;
|
2021-05-31 13:32:58 +02:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\Importer\FetchData\InvalidResponseException;
|
2021-02-01 14:14:05 +01:00
|
|
|
|
|
|
|
class FetchData
|
|
|
|
{
|
2021-04-13 14:43:04 +02:00
|
|
|
/**
|
|
|
|
* @var RequestFactoryInterface
|
|
|
|
*/
|
|
|
|
private $requestFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ClientInterface
|
|
|
|
*/
|
|
|
|
private $httpClient;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var CacheFrontendInterface
|
|
|
|
*/
|
|
|
|
private $cache;
|
2021-02-03 15:05:35 +01:00
|
|
|
|
2021-05-31 13:32:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $databaseUrlPrefix = 'https://cdb.thuecat.org';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $urlPrefix = 'https://thuecat.org';
|
|
|
|
|
2021-02-01 14:14:05 +01:00
|
|
|
public function __construct(
|
|
|
|
RequestFactoryInterface $requestFactory,
|
2021-02-16 15:08:15 +01:00
|
|
|
ClientInterface $httpClient,
|
|
|
|
CacheFrontendInterface $cache
|
2021-02-01 14:14:05 +01:00
|
|
|
) {
|
|
|
|
$this->requestFactory = $requestFactory;
|
|
|
|
$this->httpClient = $httpClient;
|
2021-02-16 15:08:15 +01:00
|
|
|
$this->cache = $cache;
|
2021-02-01 14:14:05 +01:00
|
|
|
}
|
|
|
|
|
2021-05-31 13:32:58 +02:00
|
|
|
public function updatedNodes(string $scopeId): array
|
|
|
|
{
|
|
|
|
return $this->jsonLDFromUrl(
|
|
|
|
$this->databaseUrlPrefix
|
|
|
|
. '/api/ext-sync/get-updated-nodes?syncScopeId='
|
|
|
|
. urlencode($scopeId)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-01 14:14:05 +01:00
|
|
|
public function jsonLDFromUrl(string $url): array
|
|
|
|
{
|
2021-02-16 15:08:15 +01:00
|
|
|
$cacheIdentifier = sha1($url);
|
|
|
|
$cacheEntry = $this->cache->get($cacheIdentifier);
|
|
|
|
if (is_array($cacheEntry)) {
|
|
|
|
return $cacheEntry;
|
|
|
|
}
|
|
|
|
|
2021-02-01 14:14:05 +01:00
|
|
|
$request = $this->requestFactory->createRequest('GET', $url);
|
|
|
|
$response = $this->httpClient->sendRequest($request);
|
|
|
|
|
2021-05-31 13:32:58 +02:00
|
|
|
$this->handleInvalidResponse($response, $request);
|
|
|
|
|
2021-02-16 15:08:15 +01:00
|
|
|
$jsonLD = json_decode((string) $response->getBody(), true);
|
|
|
|
if (is_array($jsonLD)) {
|
|
|
|
$this->cache->set($cacheIdentifier, $jsonLD);
|
|
|
|
return $jsonLD;
|
2021-02-01 14:14:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
2021-05-31 13:32:58 +02:00
|
|
|
|
|
|
|
public function getResourceEndpoint(): string
|
|
|
|
{
|
|
|
|
return $this->urlPrefix . '/resources/';
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handleInvalidResponse(
|
|
|
|
ResponseInterface $response,
|
|
|
|
RequestInterface $request
|
|
|
|
): void {
|
|
|
|
if ($response->getStatusCode() === 200) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($response->getStatusCode() === 401) {
|
|
|
|
throw new InvalidResponseException(
|
|
|
|
'Unauthorized API request, ensure apiKey is properly configured.',
|
|
|
|
1622461709
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($response->getStatusCode() === 404) {
|
|
|
|
throw new InvalidResponseException(
|
|
|
|
sprintf(
|
|
|
|
'Not found, given resource could not be found: "%s".',
|
|
|
|
$request->getUri()
|
|
|
|
),
|
|
|
|
1622461820
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 14:14:05 +01:00
|
|
|
}
|