events/Classes/Service/DestinationDataImportService/Slugger.php

121 lines
3.9 KiB
PHP
Raw Normal View History

2022-11-23 10:59:55 +01:00
<?php
declare(strict_types=1);
/*
* Copyright (C) 2022 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\Events\Service\DestinationDataImportService;
2022-11-23 10:59:55 +01:00
use Generator;
use PDO;
2022-11-23 10:59:55 +01:00
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\DataHandling\SlugHelper;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use WerkraumMedia\Events\Service\DestinationDataImportService\Slugger\Registry;
use WerkraumMedia\Events\Service\DestinationDataImportService\Slugger\SluggerType;
2022-11-23 10:59:55 +01:00
final class Slugger
2022-11-23 10:59:55 +01:00
{
public function __construct(
private readonly Registry $registry,
private readonly ConnectionPool $connectionPool
2022-11-23 10:59:55 +01:00
) {
}
public function update(string $tableName): void
{
$sluggerType = $this->registry->get($tableName);
foreach ($this->getRecords($sluggerType) as $record) {
$this->updateRecord($sluggerType, $record);
}
}
/**
* @return Generator<array>
2022-11-23 10:59:55 +01:00
*/
private function getRecords(SluggerType $sluggerType): Generator
2022-11-23 10:59:55 +01:00
{
$tableName = $sluggerType->getSupportedTableName();
$slugColumn = $sluggerType->getSlugColumn();
$queryBuilder = $this->getQueryBuilder($tableName);
$statement = $queryBuilder->select('*')
->from($tableName)
->where(
$queryBuilder->expr()->or(
$queryBuilder->expr()->eq($slugColumn, $queryBuilder->createNamedParameter('', PDO::PARAM_STR)),
2022-11-23 10:59:55 +01:00
$queryBuilder->expr()->isNull($slugColumn)
)
)
->executeQuery()
;
2022-11-23 10:59:55 +01:00
while ($record = $statement->fetch()) {
if (is_array($record) === false) {
continue;
}
yield $record;
}
}
private function updateRecord(SluggerType $sluggerType, array $record): void
{
$tableName = $sluggerType->getSupportedTableName();
$record = $sluggerType->prepareRecordForSlugGeneration($record);
$slug = $this->getSlugHelper($sluggerType)->generate($record, (int)$record['pid']);
2022-11-23 10:59:55 +01:00
$queryBuilder = $this->getQueryBuilder($tableName);
$queryBuilder->update($tableName)
->where(
$queryBuilder->expr()->eq(
'uid',
$queryBuilder->createNamedParameter($record['uid'], PDO::PARAM_INT)
2022-11-23 10:59:55 +01:00
)
)
->set($sluggerType->getSlugColumn(), $slug)
;
$queryBuilder->executeStatement();
2022-11-23 10:59:55 +01:00
}
private function getSlugHelper(
SluggerType $sluggerType
): SlugHelper {
$tableName = $sluggerType->getSupportedTableName();
$column = $sluggerType->getSlugColumn();
return GeneralUtility::makeInstance(
SlugHelper::class,
$tableName,
$column,
$GLOBALS['TCA'][$tableName]['columns'][$column]['config']
);
}
private function getQueryBuilder(
string $tableName
): QueryBuilder {
$queryBuilder = $this->connectionPool->getQueryBuilderForTable($tableName);
$queryBuilder->getRestrictions()->removeAll();
return $queryBuilder;
}
}