Skip to content

Commit

Permalink
Refactor process creation, add enumerations and improve code readability
Browse files Browse the repository at this point in the history
The commit is a major refactor that introduces enumerations to represent
the creator's status, abstracts process creation into a separate
service, and enhances code readability and organization. It also
includes updates to composer dependencies and brings minor changes to
various class files, improving overall code quality and maintainability.
Changes involving deprecated items are also reflected. The update is
part of an ongoing effort to streamline processes and improve code
quality.
  • Loading branch information
calien666 committed May 16, 2024
1 parent bac487d commit 01a0147
Show file tree
Hide file tree
Showing 21 changed files with 718 additions and 256 deletions.
190 changes: 5 additions & 185 deletions Classes/Cli/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,18 @@

namespace SUDHAUS7\Sudhaus7Wizard\Cli;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Exception;
use SUDHAUS7\Sudhaus7Wizard\Domain\Model\Creator;
use SUDHAUS7\Sudhaus7Wizard\Domain\Repository\CreatorRepository;
use SUDHAUS7\Sudhaus7Wizard\Services\CreateProcessFactory;
use SUDHAUS7\Sudhaus7Wizard\Tools;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException;
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* @deprecated
*/
final class RunCommand extends Command
{
public ?ConsoleLogger $logger = null;
private ?CreatorRepository $repository = null;

protected function configure(): void
{
$this->setDescription('Baukasten Wizard');
Expand All @@ -48,186 +35,19 @@ protected function configure(): void
$this->addOption('mapto', 'm', InputOption::VALUE_REQUIRED, 'write the map to this folder');
}

protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->logger = new ConsoleLogger($output);
$this->repository = GeneralUtility::makeInstance(CreatorRepository::class);
}

/**
* @throws \Doctrine\DBAL\Driver\Exception
* @throws DBALException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$mapFolder = null;
if ($input->getOption('mapto')) {
$mapFolder = $input->getOption('mapto');
}

switch ($input->getArgument('mode')) {
case 'info':
if ($input->getOption('id')) {
$force = false;
if ($input->getOption('force')) {
$force = true;
}
$o = $this->repository->findByIdentifier($input->getOption('id'), $force);
if ($o instanceof Creator) {
$this->getInfo($o, $input, $output);
return Command::SUCCESS;
} else {
$output->writeln('<info>Not found</info>');
}
} else {
$o = $this->repository->findNext();
if ($o instanceof Creator) {
$this->getInfo($o, $input, $output);
return Command::SUCCESS;
}
}
break;
case 'single':
if ($input->getOption('id')) {
$force = false;
if ($input->getOption('force')) {
$force = true;
}
$o = $this->repository->findByIdentifier($input->getOption('id'), $force);
if ($o instanceof Creator) {
return $this->create($o, $input, $output, $mapFolder);
}
}
return 0;
case 'status':
$this->getStatus($input, $output);
return 0;
case 'list':
$this->getList($input, $output);
return 0;
case 'next':
$o = $this->repository->findNext();
if ($o instanceof Creator) {
if (!$this->repository->isRunning()) {
return $this->create($o, $input, $output, $mapFolder);
}
} else {
$output->writeln('<info>🎆 No wizard jobs to process 🎆</info>');
return Command::SUCCESS;
}
break;
default:
break;
}
return 1;
}

/**
* @deprecated
*/
private function forceVisible(int $id): void
{
GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_sudhaus7wizard_domain_model_creator')
->update(
'tx_sudhaus7wizard_domain_model_creator',
['uid' => $id],
['hidden' => 0, 'deleted' => 0, 'status' => 10]
);
}

public function getInfo(Creator $o, InputInterface $input, OutputInterface $output): void
{
$output->write(sprintf("Generiere Baukasten %s\n", $o->getLongname()));
$output->write("Vorlage:\t" . $o->getBase() . "\n");
$output->write("Projektname:\t" . $o->getProjektname() . "\n");
$output->write("Kurzname:\t" . $o->getShortname() . "\n");
$output->write("Domain:\t" . $o->getDomainname() . "\n");
$output->write("Kontakt:\t" . $o->getContact() . "\n");
$output->write("Benutzer:\t" . $o->getReduser() . ' ' . $o->getRedemail() . "\n");

$a = $o->getFlexinfo();
foreach ($a['data']['sDEF']['lDEF'] as $k => $v) {
$output->write(sprintf("%s:\t%s\n", ucfirst((string)$k), $v['vDEF']));
}
}

public function create(Creator $creator, InputInterface $input, OutputInterface $output, $mapfolder = null): int
{
Bootstrap::initializeBackendAuthentication();
$creator->setStatus(15);
$this->repository->updateStatus($creator);
$this->getInfo($creator, $input, $output);
//$output->write(implode("\n",)."\n");

try {
if (CreateProcessFactory::get($creator, $this->logger)->run($mapfolder)) {
$output->write("Fertig\n", true);
$creator->setStatus(20);

$this->repository->updateStatus($creator);
$this->repository->updatePid($creator);

$user = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('be_users')
->select(
['*'],
'be_users',
['uid' => $creator->getCruserId()],
[],
[],
1
)
->fetchAssociative();

if (!empty($user['email'])) {
// Create the message
/** @var MailMessage $mail */
$mail = GeneralUtility::makeInstance(MailMessage::class);

// Prepare and send the message
$mail->setSubject(sprintf('[Wizard] %s ist fertig', $creator->getProjektname()))
->setFrom($user['email'])
->setTo($user['email'])
->text(sprintf('Der neue Baukasten %s wurde angelegt', $creator->getProjektname()));
$mail->send();
$output->write("E-Mail versendet\n");
}

return Command::SUCCESS;
}
} catch (Exception|ExtensionConfigurationExtensionNotConfiguredException|ExtensionConfigurationPathDoesNotExistException $e) {
$this->logger->warning($e->getMessage(), $e->getTrace());
}

$creator->setStatus(17);
$this->repository->updateStatus($creator);

return Command::FAILURE;
$this->getStatus($input, $output);
return 0;
}

public function getStatus(InputInterface $input, OutputInterface $output): void
{
//$this->mylist($input, $output);
$output->writeln([
'-------------------------------------',
print_r(Tools::getRegisteredExtensions(), true),
print_r(Tools::getCreatorConfig(), true),

], $output::VERBOSITY_NORMAL);
}

public function getList(InputInterface $input, OutputInterface $output): void
{
$table = new Table($output);
$table->setHeaderTitle('Todo List');
$table->setHeaders(['ID', 'Baukasten', 'Status']);

$list = $this->repository->findAll();
/** @var $o Creator */
foreach ($list as $o) {
$table->addRow([$o->getUid(), $o->getLongname(), $o->getStatusLabel()]);
}
$table->render();
}
}
38 changes: 38 additions & 0 deletions Classes/Command/ListNextCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace SUDHAUS7\Sudhaus7Wizard\Command;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Exception;
use SUDHAUS7\Sudhaus7Wizard\Service\ProcessService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class ListNextCommand extends Command
{
public function __construct(
private ProcessService $processService,
?string $name = null
) {
parent::__construct($name);
}

protected function configure(): void
{
$this->setDescription('SUDHAUS7 Wizard. Shows list of not executed processes');
}

/**
* @throws Exception
* @throws DBALException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->processService->printList($output);

return Command::SUCCESS;
}
}
103 changes: 103 additions & 0 deletions Classes/Command/RunWizardCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace SUDHAUS7\Sudhaus7Wizard\Command;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Exception;
use SUDHAUS7\Sudhaus7Wizard\Domain\Dto\Process;
use SUDHAUS7\Sudhaus7Wizard\Domain\Model\Creator;
use SUDHAUS7\Sudhaus7Wizard\Domain\Repository\CreatorRepository;
use SUDHAUS7\Sudhaus7Wizard\Service\ProcessService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Utility\GeneralUtility;

final class RunWizardCommand extends Command
{
public function __construct(
private CreatorRepository $creatorRepository,
private ConsoleLogger $logger,
private ProcessService $processService,
?string $name = null
) {
parent::__construct($name);
}

protected function configure(): void
{
$this->setDefinition([
new InputOption(
'id',
'i',
InputOption::VALUE_OPTIONAL,
'ID for executing specific single wizard task',
null
),
new InputOption(
'force',
'f',
InputOption::VALUE_NONE,
'force execution of specific task, works only with --id'
),
new InputOption(
'mapto',
'm',
InputOption::VALUE_OPTIONAL,
'If set, write page and content mapping CSV to the specific folder',
null
),
])
->setDescription('SUDHAUS7 Wizard. Helps you to create new pages from a given template. If no options are given, executes the next task in pipeline.');
}
protected function initialize(
InputInterface $input,
OutputInterface $output
): void {
parent::initialize($input, $output);
}

/**
* @throws Exception
* @throws DBALException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$ioHelper = new SymfonyStyle($input, $output);
if ($this->creatorRepository->isRunning()) {
$ioHelper->warning('Wizard has currently running processes');
return Command::FAILURE;
}
$taskId = $input->getOption('id');
$forceExecution = $input->getOption('force');
$mappingFolder = $input->getOption('mapto');
$cleanedUpMappingFolderPath = GeneralUtility::getFileAbsFileName($mappingFolder);
if ($mappingFolder !== '' && $cleanedUpMappingFolderPath === '') {
$ioHelper->error('You defined an invalid mapping folder path. Please check and try again.');
return Command::FAILURE;
}
if ($taskId !== null) {
$creator = $this->creatorRepository->findByIdentifier($taskId, (bool)$forceExecution);
} else {
$creator = $this->creatorRepository->findNext();
}
if (!$creator instanceof Creator) {
$ioHelper->info('🎆 No wizard jobs to process 🎆');
return Command::SUCCESS;
}

Bootstrap::initializeBackendAuthentication();
$process = new Process($creator, $this->logger, $cleanedUpMappingFolderPath);

if ($this->processService->create($process, $ioHelper) === Command::SUCCESS) {
return Command::SUCCESS;
}
return Command::FAILURE;
}
}
Loading

0 comments on commit 01a0147

Please sign in to comment.