DigiComp.Sequence/Classes/Command/SequenceCommandController.php

84 lines
2.3 KiB
PHP
Raw Normal View History

2014-04-07 15:25:16 +02:00
<?php
declare(strict_types=1);
2014-04-07 15:25:16 +02:00
namespace DigiComp\Sequence\Command;
2021-09-22 11:34:34 +02:00
use DigiComp\Sequence\Domain\Model\SequenceEntry;
use DigiComp\Sequence\Service\Exception\InvalidSourceException;
2017-03-13 16:59:04 +01:00
use DigiComp\Sequence\Service\SequenceGenerator;
2021-09-21 10:39:03 +02:00
use Doctrine\DBAL\Driver\Exception as DoctrineDBALDriverException;
use Doctrine\DBAL\Exception as DoctrineDBALException;
use Doctrine\ORM\EntityManagerInterface;
2017-03-13 16:59:04 +01:00
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
2014-04-07 15:25:16 +02:00
/**
* @Flow\Scope("singleton")
*/
class SequenceCommandController extends CommandController
2016-06-24 19:40:43 +02:00
{
/**
* @Flow\Inject
2021-08-26 10:50:00 +02:00
* @var SequenceGenerator
2016-06-24 19:40:43 +02:00
*/
protected $sequenceGenerator;
2014-04-07 15:25:16 +02:00
/**
* @Flow\Inject
* @var EntityManagerInterface
*/
protected $entityManager;
2016-06-24 19:40:43 +02:00
/**
2021-09-22 11:34:34 +02:00
* Set last number for sequence generator.
2016-06-24 19:40:43 +02:00
*
* @param string $type
2021-09-22 11:34:34 +02:00
* @param int $number
* @throws DoctrineDBALDriverException
* @throws DoctrineDBALException
* @throws InvalidSourceException
2016-06-24 19:40:43 +02:00
*/
2021-09-22 11:34:34 +02:00
public function setLastNumberForCommand(string $type, int $number): void
2016-06-24 19:40:43 +02:00
{
2021-09-22 11:34:34 +02:00
if ($this->sequenceGenerator->setLastNumberFor($type, $number)) {
$this->outputLine('Last number successfully set.');
} else {
$this->outputLine('Failed to set last number.');
}
2016-06-24 19:40:43 +02:00
}
/**
2021-09-22 11:34:34 +02:00
* Clean up sequence table.
*
* @param string[] $types
2021-09-21 10:39:03 +02:00
* @throws DoctrineDBALDriverException
* @throws DoctrineDBALException
2021-09-22 11:34:34 +02:00
* @throws InvalidSourceException
*/
2021-09-22 11:34:34 +02:00
public function cleanUpCommand(array $types = []): void
{
2021-09-22 11:34:34 +02:00
if ($types === []) {
foreach (
$this
->entityManager
->createQuery('SELECT DISTINCT(se.type) type FROM ' . SequenceEntry::class . ' se')
->execute()
as $result
) {
$types[] = $result['type'];
}
}
2021-09-22 11:34:34 +02:00
foreach ($types as $type) {
$rowCount = $this
->entityManager
->createQuery('DELETE FROM ' . SequenceEntry::class . ' se WHERE se.type = ?0 AND se.number < ?1')
->execute([$type, $this->sequenceGenerator->getLastNumberFor($type)]);
$this->outputLine('Deleted ' . $rowCount . ' row(s) for type "' . $type . '".');
}
}
2014-04-07 15:25:16 +02:00
}