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;
|
|
|
|
|
2021-02-01 14:14:05 +01:00
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
2021-08-05 15:18:39 +02:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\EntityMapper\EntityRegistry;
|
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\EntityMapper\JsonDecode;
|
|
|
|
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\ImportConfiguration;
|
|
|
|
use WerkraumMedia\ThueCat\Domain\Model\Backend\ImportLog;
|
|
|
|
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 ImportLog
|
|
|
|
*/
|
|
|
|
private $importLog;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ImportLogRepository
|
|
|
|
*/
|
|
|
|
private $importLogRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ImportConfiguration
|
|
|
|
*/
|
|
|
|
private $configuration;
|
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,
|
|
|
|
SaveData $saveData
|
|
|
|
) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function importConfiguration(ImportConfiguration $configuration): ImportLog
|
|
|
|
{
|
2021-02-17 15:33:15 +01:00
|
|
|
$this->configuration = $configuration;
|
2021-02-01 14:14:05 +01:00
|
|
|
|
2021-02-17 15:33:15 +01:00
|
|
|
$this->importLog = GeneralUtility::makeInstance(ImportLog::class, $this->configuration);
|
|
|
|
|
|
|
|
$urlProvider = $this->urls->getProviderForConfiguration($this->configuration);
|
2021-02-03 15:05:35 +01:00
|
|
|
if (!$urlProvider instanceof UrlProvider) {
|
|
|
|
return $this->importLog;
|
|
|
|
}
|
|
|
|
|
2021-02-01 14:14:05 +01:00
|
|
|
foreach ($urlProvider->getUrls() as $url) {
|
|
|
|
$this->importResourceByUrl($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->importLogRepository->addLog($this->importLog);
|
|
|
|
return clone $this->importLog;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function importResourceByUrl(string $url): void
|
|
|
|
{
|
|
|
|
$content = $this->fetchData->jsonLDFromUrl($url);
|
|
|
|
|
|
|
|
if ($content === []) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($content['@graph'] as $jsonEntity) {
|
|
|
|
$this->importJsonEntity($jsonEntity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function importJsonEntity(array $jsonEntity): void
|
|
|
|
{
|
2021-08-05 15:18:39 +02:00
|
|
|
$targetEntity = $this->entityRegistry->getEntityByTypes($jsonEntity['@type']);
|
|
|
|
if ($targetEntity === '') {
|
2021-02-01 14:14:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-08-05 15:18:39 +02:00
|
|
|
|
|
|
|
$entities = new EntityCollection();
|
|
|
|
|
|
|
|
foreach ($this->languages->getAvailable($this->configuration) as $language) {
|
|
|
|
$mappedEntity = $this->entityMapper->mapDataToEntity(
|
|
|
|
$jsonEntity,
|
|
|
|
$targetEntity,
|
|
|
|
[
|
|
|
|
JsonDecode::ACTIVE_LANGUAGE => $language,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
if (!$mappedEntity instanceof MapsToType) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$convertedEntity = $this->converter->convert(
|
|
|
|
$mappedEntity,
|
|
|
|
$this->configuration,
|
|
|
|
$language
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($convertedEntity === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$entities->add($convertedEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->saveData->import(
|
|
|
|
$entities,
|
|
|
|
$this->importLog
|
|
|
|
);
|
2021-02-01 14:14:05 +01:00
|
|
|
}
|
|
|
|
}
|