WIP|Prevent endless loops

This commit is contained in:
Daniel Siepmann 2023-01-16 09:09:28 +01:00
parent 45eda76e98
commit d988d4bd33
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
6 changed files with 114 additions and 10 deletions

View file

@ -143,13 +143,14 @@ class Importer
}
foreach ($urlProvider->getUrls() as $url) {
$this->logger->info('Process url.', ['url' => $url]);
$this->importResourceByUrl($url);
$this->logger->info('Finished url.', ['url' => $url]);
}
}
private function importResourceByUrl(string $url): void
{
$this->logger->info('Process url.', ['url' => $url]);
if ($this->import->handledRemoteId($url)) {
$this->logger->notice('Skip Url as we already handled it during import.', ['url' => $url]);
return;

View file

@ -46,6 +46,7 @@ use WerkraumMedia\ThueCat\Domain\Import\Model\Entity;
use WerkraumMedia\ThueCat\Domain\Import\Model\GenericEntity;
use WerkraumMedia\ThueCat\Domain\Import\ResolveForeignReference;
use WerkraumMedia\ThueCat\Domain\Model\Backend\ImportConfiguration;
use WerkraumMedia\ThueCat\Domain\Model\Backend\RequestedImportType;
use WerkraumMedia\ThueCat\Domain\Repository\Backend\OrganisationRepository;
use WerkraumMedia\ThueCat\Domain\Repository\Backend\ParkingFacilityRepository;
use WerkraumMedia\ThueCat\Domain\Repository\Backend\TownRepository;
@ -109,6 +110,14 @@ class GeneralConverter implements Converter
Organisation::class => 'tx_thuecat_organisation',
];
/**
* @var string[]
*/
private $tablesAllowedWithoutManager = [
'tx_thuecat_organisation',
'tx_thuecat_town',
];
public function __construct(
ResolveForeignReference $resolveForeignReference,
Importer $importer,
@ -200,8 +209,11 @@ class GeneralConverter implements Converter
return false;
}
if ($tableName !== 'tx_thuecat_organisation' && $this->getManagerUid($entity) === '') {
$this->logger->info('Skipped conversion of entity, is not an organisation and no manager was available', [
if (
in_array($tableName, $this->tablesAllowedWithoutManager) === false
&& $this->getManagerUid($entity) === ''
) {
$this->logger->info('Skipped conversion of entity, is not an record type that is allowed without manager and no manager was available', [
'remoteId' => $entity->getId(),
]);
return false;
@ -259,19 +271,28 @@ class GeneralConverter implements Converter
];
}
private function getManagerUid(object $entity): string
private function getManagerUid(Minimum $entity): string
{
if ($this->importConfiguration->getRequestedImportType()->equals(RequestedImportType::MANAGER)) {
$this->logger->info('Skipped fetching manager uid. We already try to import a manager and do not import managers for managers.', ['url' => $entity->getId()]);
return '';
}
if (
method_exists($entity, 'getManagedBy') === false
|| !$entity->getManagedBy() instanceof ForeignReference
) {
$this->logger->info('Skipped fetching manager uid. The entity did not provide a getManagedBy method or the method did not return a ForeignReference.', ['url' => $entity->getId()]);
return '';
}
$this->logger->info('Start import for manager.', ['urls' => ResolveForeignReference::convertToRemoteIds([$entity->getManagedBy()])]);
$this->importer->importConfiguration(
ImportConfiguration::createFromBaseWithForeignReferences(
$this->importConfiguration,
[$entity->getManagedBy()]
[$entity->getManagedBy()],
[],
RequestedImportType::MANAGER
)
);
$manager = $this->organisationRepository->findOneByRemoteId(
@ -281,20 +302,27 @@ class GeneralConverter implements Converter
return $manager ? (string)$manager->getUid() : '';
}
private function getTownUid(object $entity): string
private function getTownUid(Minimum $entity): string
{
if ($this->importConfiguration->getRequestedImportType()->equals(RequestedImportType::TOWN)) {
$this->logger->info('Skipped fetching town uid. We already try to import a town and do not import town for town.', ['url' => $entity->getId()]);
return '';
}
if (
$entity instanceof Town
|| method_exists($entity, 'getContainedInPlaces') === false
) {
$this->logger->info('Skipped fetching town uid. The entity did not provide a getContainedInPlaces method or was a town itself.', ['url' => $entity->getId()]);
return '';
}
$this->logger->info('Start import for town.', ['urls' => ResolveForeignReference::convertToRemoteIds($entity->getContainedInPlaces())]);
$this->importer->importConfiguration(
ImportConfiguration::createFromBaseWithForeignReferences(
$this->importConfiguration,
$entity->getContainedInPlaces(),
['thuecat:Town']
['thuecat:Town'],
RequestedImportType::TOWN
)
);
$town = $this->townRepository->findOneByEntity($entity);
@ -307,6 +335,7 @@ class GeneralConverter implements Converter
return [];
}
$this->logger->info('Start import for parking facilities near by.', ['urls' => ResolveForeignReference::convertToRemoteIds($entity->getParkingFacilitiesNearBy())]);
$this->importer->importConfiguration(
ImportConfiguration::createFromBaseWithForeignReferences(
$this->importConfiguration,
@ -320,7 +349,7 @@ class GeneralConverter implements Converter
}
private function getAccessibilitySpecification(
object $entity,
Minimum $entity,
string $language
): string {
if (

View file

@ -66,11 +66,22 @@ class ImportConfiguration extends AbstractEntity implements ImportConfigurationI
/**
* @var string[]
*/
protected $allowedTypes = [];
private $allowedTypes = [];
/**
* @var RequestedImportType
*/
private $requestedImportType = null;
public function __construct()
{
$this->initializeObject();
}
public function initializeObject(): void
{
$this->logs = new ObjectStorage();
$this->requestedImportType = new RequestedImportType();
}
public function getTitle(): string
@ -142,6 +153,11 @@ class ImportConfiguration extends AbstractEntity implements ImportConfigurationI
return $this->allowedTypes;
}
public function getRequestedImportType(): RequestedImportType
{
return $this->requestedImportType;
}
public function getSyncScopeId(): string
{
return $this->getConfigurationValueFromFlexForm('syncScopeId');
@ -181,12 +197,14 @@ class ImportConfiguration extends AbstractEntity implements ImportConfigurationI
public static function createFromBaseWithForeignReferences(
self $base,
array $foreignReferences,
array $allowedTypes = []
array $allowedTypes = [],
string $requestedImportType = ''
): self {
$configuration = clone $base;
$configuration->urls = ResolveForeignReference::convertToRemoteIds($foreignReferences);
$configuration->type = 'static';
$configuration->allowedTypes = $allowedTypes;
$configuration->requestedImportType = new RequestedImportType($requestedImportType);
return $configuration;
}

View file

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2023 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.
*/
namespace WerkraumMedia\ThueCat\Domain\Model\Backend;
use TYPO3\CMS\Core\Type\Enumeration;
/**
* Indicates that a domain specific type was requested to be imported.
*
* This can be used for further checks during import, e.g. abort recursive imports if a manager needs a manager.
*/
class RequestedImportType extends Enumeration
{
const __default = self::NONE;
const NONE = '';
const MANAGER = 'manager';
const TOWN = 'town';
}

View file

@ -15,3 +15,4 @@ Those changes are documented so we know what to do once we drop an older version
:reversed:
Maintenance/PHP/*
Maintenance/TYPO3/*

View file

@ -0,0 +1,13 @@
TYPO3 11.5
==========
Changes that should happen once we drop TYPO3 11.5.
Remove ``initializeObject()`` methods
-------------------------------------
Those are necessary prior v12 in order to properly initialize none re-constructed
data within Extbase models.
This vanished with v12 where ``makeInstance()`` is used which will call the ``__construct()`` method.
The content of the method should move to ``__construct()`` and the method should be removed.