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;
|
|
|
|
|
2022-12-15 10:42:41 +01:00
|
|
|
use TYPO3\CMS\Core\Log\LogManager;
|
|
|
|
use TYPO3\CMS\Core\Log\Logger;
|
2021-08-05 15:18:39 +02:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\EntityMapper\EntityRegistry;
|
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\EntityMapper\JsonDecode;
|
2022-12-15 12:41:35 +01:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\EntityMapper\MappingException;
|
2021-08-05 15:18:39 +02:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\Entity\MapsToType;
|
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\Importer\Converter;
|
2021-02-01 14:14:05 +01:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\Importer\FetchData;
|
2021-08-05 15:18:39 +02:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\Importer\Languages;
|
2021-02-01 14:14:05 +01:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\Importer\SaveData;
|
2021-08-05 15:18:39 +02:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\Model\EntityCollection;
|
2021-02-01 14:14:05 +01:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\UrlProvider\Registry as UrlProviderRegistry;
|
2021-02-03 15:05:35 +01:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\UrlProvider\UrlProvider;
|
2021-02-01 14:14:05 +01:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Model\Backend\ImportLog;
|
2022-12-15 12:41:35 +01:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Model\Backend\ImportLogEntry\MappingError;
|
2021-02-01 14:14:05 +01:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Repository\Backend\ImportLogRepository;
|
|
|
|
|
|
|
|
class Importer
|
|
|
|
{
|
2021-04-13 14:43:04 +02:00
|
|
|
/**
|
|
|
|
* @var UrlProviderRegistry
|
|
|
|
*/
|
|
|
|
private $urls;
|
|
|
|
|
|
|
|
/**
|
2021-08-05 15:18:39 +02:00
|
|
|
* @var Converter
|
2021-04-13 14:43:04 +02:00
|
|
|
*/
|
|
|
|
private $converter;
|
|
|
|
|
2021-08-05 15:18:39 +02:00
|
|
|
/**
|
|
|
|
* @var EntityRegistry
|
|
|
|
*/
|
|
|
|
private $entityRegistry;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EntityMapper
|
|
|
|
*/
|
|
|
|
private $entityMapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Languages
|
|
|
|
*/
|
|
|
|
private $languages;
|
|
|
|
|
2021-04-13 14:43:04 +02:00
|
|
|
/**
|
|
|
|
* @var FetchData
|
|
|
|
*/
|
|
|
|
private $fetchData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SaveData
|
|
|
|
*/
|
|
|
|
private $saveData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ImportLogRepository
|
|
|
|
*/
|
|
|
|
private $importLogRepository;
|
|
|
|
|
2022-12-15 10:42:41 +01:00
|
|
|
/**
|
|
|
|
* @var Logger
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
2021-04-13 14:43:04 +02:00
|
|
|
/**
|
2021-08-18 13:54:18 +02:00
|
|
|
* @var Import
|
2021-04-13 14:43:04 +02:00
|
|
|
*/
|
2021-08-18 13:54:18 +02:00
|
|
|
private $import;
|
2021-02-01 14:14:05 +01:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
UrlProviderRegistry $urls,
|
2021-08-05 15:18:39 +02:00
|
|
|
Converter $converter,
|
|
|
|
EntityRegistry $entityRegistry,
|
|
|
|
EntityMapper $entityMapper,
|
|
|
|
Languages $languages,
|
2021-02-01 14:14:05 +01:00
|
|
|
ImportLogRepository $importLogRepository,
|
|
|
|
FetchData $fetchData,
|
2022-12-15 10:42:41 +01:00
|
|
|
SaveData $saveData,
|
|
|
|
LogManager $logManager
|
2021-02-01 14:14:05 +01:00
|
|
|
) {
|
|
|
|
$this->urls = $urls;
|
|
|
|
$this->converter = $converter;
|
2021-08-05 15:18:39 +02:00
|
|
|
$this->entityRegistry = $entityRegistry;
|
|
|
|
$this->entityMapper = $entityMapper;
|
|
|
|
$this->languages = $languages;
|
2021-02-01 14:14:05 +01:00
|
|
|
$this->importLogRepository = $importLogRepository;
|
|
|
|
$this->fetchData = $fetchData;
|
|
|
|
$this->saveData = $saveData;
|
2022-12-15 10:42:41 +01:00
|
|
|
$this->logger = $logManager->getLogger(__CLASS__);
|
2021-08-18 13:54:18 +02:00
|
|
|
$this->import = new Import();
|
2021-02-01 14:14:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function importConfiguration(ImportConfiguration $configuration): ImportLog
|
|
|
|
{
|
2021-08-18 13:54:18 +02:00
|
|
|
$this->import->start($configuration);
|
|
|
|
$this->import();
|
|
|
|
$this->import->end();
|
|
|
|
|
|
|
|
if ($this->import->done()) {
|
2022-12-15 10:42:41 +01:00
|
|
|
$this->logger->info(
|
|
|
|
'Finished import.',
|
|
|
|
[
|
|
|
|
'errors' => $this->import->getLog()->getListOfErrors(),
|
|
|
|
'summary' => $this->import->getLog()->getSummaryOfEntries(),
|
|
|
|
]
|
|
|
|
);
|
2021-08-18 13:54:18 +02:00
|
|
|
$this->importLogRepository->addLog($this->import->getLog());
|
|
|
|
}
|
2021-02-01 14:14:05 +01:00
|
|
|
|
2021-08-18 13:54:18 +02:00
|
|
|
return $this->import->getLog();
|
|
|
|
}
|
2021-02-17 15:33:15 +01:00
|
|
|
|
2021-08-18 13:54:18 +02:00
|
|
|
private function import(): void
|
|
|
|
{
|
|
|
|
$urlProvider = $this->urls->getProviderForConfiguration($this->import->getConfiguration());
|
2021-02-03 15:05:35 +01:00
|
|
|
if (!$urlProvider instanceof UrlProvider) {
|
2021-08-18 13:54:18 +02:00
|
|
|
throw new \Exception('No URL Provider available for given configuration.', 1629296635);
|
2021-02-03 15:05:35 +01:00
|
|
|
}
|
|
|
|
|
2021-02-01 14:14:05 +01:00
|
|
|
foreach ($urlProvider->getUrls() as $url) {
|
|
|
|
$this->importResourceByUrl($url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function importResourceByUrl(string $url): void
|
|
|
|
{
|
2022-12-15 10:42:41 +01:00
|
|
|
$this->logger->info('Process url.', ['url' => $url]);
|
2021-08-18 13:54:18 +02:00
|
|
|
if ($this->import->handledRemoteId($url)) {
|
2022-12-15 10:42:41 +01:00
|
|
|
$this->logger->notice('Skip Url as we already handled it during import.', ['url' => $url]);
|
2021-08-18 13:54:18 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-02-01 14:14:05 +01:00
|
|
|
$content = $this->fetchData->jsonLDFromUrl($url);
|
|
|
|
|
|
|
|
if ($content === []) {
|
2022-12-15 10:42:41 +01:00
|
|
|
$this->logger->notice('Skip Url as we did not receive any content.', ['url' => $url]);
|
2021-02-01 14:14:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($content['@graph'] as $jsonEntity) {
|
|
|
|
$this->importJsonEntity($jsonEntity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function importJsonEntity(array $jsonEntity): void
|
|
|
|
{
|
2021-08-18 13:54:18 +02:00
|
|
|
if ($this->entityAllowed($jsonEntity) === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-05 15:18:39 +02:00
|
|
|
$targetEntity = $this->entityRegistry->getEntityByTypes($jsonEntity['@type']);
|
|
|
|
if ($targetEntity === '') {
|
2022-12-15 10:42:41 +01:00
|
|
|
$this->logger->notice('Skip entity, no target entity found.', ['types' => $jsonEntity['@type']]);
|
2021-02-01 14:14:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-08-05 15:18:39 +02:00
|
|
|
|
|
|
|
$entities = new EntityCollection();
|
|
|
|
|
2021-08-18 13:54:18 +02:00
|
|
|
foreach ($this->languages->getAvailable($this->import->getConfiguration()) as $language) {
|
2022-12-15 10:42:41 +01:00
|
|
|
$this->logger->info('Process entity for language.', ['language' => $language, 'targetEntity' => $targetEntity]);
|
2022-12-15 12:41:35 +01:00
|
|
|
try {
|
|
|
|
$mappedEntity = $this->entityMapper->mapDataToEntity(
|
|
|
|
$jsonEntity,
|
|
|
|
$targetEntity,
|
|
|
|
[
|
|
|
|
JsonDecode::ACTIVE_LANGUAGE => $language,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (MappingException $e) {
|
|
|
|
$this->logger->error('Could not map data to entity.', [
|
|
|
|
'url' => $e->getUrl(),
|
|
|
|
'language' => $language,
|
|
|
|
'mappingError' => $e->getMessage(),
|
|
|
|
]);
|
|
|
|
$this->import->getLog()->addEntry(new MappingError($e));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-08-05 15:18:39 +02:00
|
|
|
if (!$mappedEntity instanceof MapsToType) {
|
2022-12-15 12:41:35 +01:00
|
|
|
$this->logger->error('Mapping did not result in an MapsToType instance.', ['class' => get_class($mappedEntity)]);
|
2021-08-05 15:18:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
2022-12-15 12:41:35 +01:00
|
|
|
|
2021-08-05 15:18:39 +02:00
|
|
|
$convertedEntity = $this->converter->convert(
|
|
|
|
$mappedEntity,
|
2021-08-18 13:54:18 +02:00
|
|
|
$this->import->getConfiguration(),
|
2021-08-05 15:18:39 +02:00
|
|
|
$language
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($convertedEntity === null) {
|
2022-12-15 12:41:35 +01:00
|
|
|
$this->logger->error('Could not convert entity.', ['language' => $language, 'targetEntity' => $targetEntity]);
|
2021-08-05 15:18:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$entities->add($convertedEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->saveData->import(
|
|
|
|
$entities,
|
2021-08-18 13:54:18 +02:00
|
|
|
$this->import->getLog()
|
2021-08-05 15:18:39 +02:00
|
|
|
);
|
2021-02-01 14:14:05 +01:00
|
|
|
}
|
2021-08-18 13:54:18 +02:00
|
|
|
|
|
|
|
private function entityAllowed(array $jsonEntity): bool
|
|
|
|
{
|
|
|
|
if ($this->import->getConfiguration()->getAllowedTypes() === []) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($jsonEntity['@type'] as $type) {
|
|
|
|
if (in_array($type, $this->import->getConfiguration()->getAllowedTypes()) === true) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 10:42:41 +01:00
|
|
|
$this->logger->notice('Deny entity as type is not allowed.', ['types' => $jsonEntity['@type']]);
|
2021-08-18 13:54:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-01 14:14:05 +01:00
|
|
|
}
|