Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
# This is the 1st commit message:
Browse files Browse the repository at this point in the history
chore: replace composer deps to begin refactor

# This is the commit message #2:

chore: replace composer deps to begin refactor

# This is the commit message #3:

refactor: replace laminas-console with laminas-cli

# This is the commit message #4:

refactor: replace laminas-console with laminas-cli

# This is the commit message #5:

refactor: replace laminas-console with laminas-cli

# This is the commit message #6:

refactor: replace laminas-console with laminas-cli

# This is the commit message #7:

refactor: near-identical factory boilerplate was bloating, moved to ConfigAbstractFactory for these commands.

# This is the commit message #8:

refactor: near-identical factory boilerplate was bloating, moved to ConfigAbstractFactory for these commands.

# This is the commit message #9:

refactor: near-identical factory boilerplate was bloating, moved to ConfigAbstractFactory for these commands.

# This is the commit message #10:

refactor: Tweaked result handling

# This is the commit message #11:

refactor: migrated more commands from BatchController

# This is the commit message #12:

refactor: migrated more commands from BatchController

# This is the commit message #13:

refactor: more cmds migrated

# This is the commit message #14:

refactor: some config organised, queue controllers spun out into commands.

# This is the commit message #15:

refactor: some fixes to config and constructor

# This is the commit message #16:

refactor: reorganised a bit to correct some incorrect behaviour noticed in local testing

# This is the commit message #17:

refactor: sorted auth/privelege escallation now running as laminas-cli not as MvcConsole request

# This is the commit message #18:

refactor: Intial unit test work.

# This is the commit message #19:

refactor: more unit test work

# This is the commit message #20:

refactor: further tests implemented

refactor: further test implemented

refactor: unit test work

refactor: more unit tests created

refactor: Tests, static analysis fixes. Refactored controllers removed.

refactor: Tests, static analysis fixes. Refactored controllers removed.

refactor: Imports optimised
  • Loading branch information
fibble committed Mar 1, 2024
1 parent aab10a0 commit aed4a2b
Show file tree
Hide file tree
Showing 86 changed files with 2,837 additions and 2,553 deletions.
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"repositories": [
{
"type": "vcs",
"url": "[email protected]:dvsa/olcs-logging.git"
}
],
"name": "olcs/backend",
"description": "Back End Application for OLCS Project",
"require": {
Expand All @@ -20,7 +26,7 @@
"league/flysystem": "^1.0",
"league/flysystem-webdav": "1.0.10",
"monolog/monolog": "^2.9",
"olcs/olcs-logging": "^6.0.0",
"olcs/olcs-logging": "dev-migrate-to-laminas-cli-vol-3615",
"olcs/olcs-transfer": "^6.0.0",
"olcs/olcs-utils": "^6.0.0",
"olcs/olcs-xmltools": "^6.0.0",
Expand All @@ -39,15 +45,15 @@
"lm-commons/lmc-rbac-mvc": "^3.3",
"laminas/laminas-mail": "^2.16",
"laminas/laminas-mvc": "^3.3",
"laminas/laminas-mvc-console": "^1.3",
"laminas/laminas-mvc-i18n": "^1.4",
"laminas/laminas-servicemanager": "^3.17",
"laminas/laminas-http": "^2.16",
"laminas/laminas-view": "^2.23",
"laminas/laminas-i18n": "^2.17",
"laminas/laminas-serializer": "^2.13",
"laminas/laminas-eventmanager": "^3.5",
"psr/container": "^1.1|^2"
"psr/container": "^1.1|^2",
"laminas/laminas-cli": "^1.5"
},
"autoload": {
"classmap": [
Expand Down
1 change: 0 additions & 1 deletion config/application.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
'Laminas\Cache\Storage\Adapter\Redis',
'Laminas\Filter\Module',
'Laminas\Validator\Module',
'Laminas\Mvc\Console',
'Laminas\Mvc\I18n\Module',
'Laminas\I18n\Module',
'Dvsa\Olcs\Utils',
Expand Down
4 changes: 2 additions & 2 deletions module/Api/src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected function logResponse(\Laminas\Stdlib\ResponseInterface $response)
$content = substr($content, 0, 1000) . '...';
}

if ($response instanceof \Laminas\Console\Response) {
if (getenv('OLCS_CLI')) {
$priority = $response->getErrorLevel() === 0 ? \Laminas\Log\Logger::DEBUG : \Laminas\Log\Logger::ERR;
Logger::log(
$priority,
Expand All @@ -92,7 +92,7 @@ protected function logResponse(\Laminas\Stdlib\ResponseInterface $response)
);
}

if ($response instanceof \Laminas\Http\PhpEnvironment\Response) {
if ($response instanceof \Laminas\Http\PhpEnvironment\Response && !getenv('OLCS_CLI')) {
Logger::logResponse(
$response->getStatusCode(),
'API Response Sent',
Expand Down
2 changes: 1 addition & 1 deletion module/Api/src/Rbac/JWTIdentityProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function getIdentity(): IdentityInterface
return $this->identity;
}

if ($this->request instanceof \Laminas\Console\Request) {
if (getenv('OLCS_CLI')) {
return $this->identity = new Identity($this->getSystemUser());
}

Expand Down
2 changes: 1 addition & 1 deletion module/Api/src/Rbac/PidIdentityProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getIdentity()
*/
private function authenticate()
{
if ($this->request instanceof \Laminas\Console\Request) {
if (getenv('OLCS_CLI')) {
$auth = IdentityProviderInterface::SYSTEM_USER;
return $this->repository->fetchById($auth);
} else {
Expand Down
417 changes: 91 additions & 326 deletions module/Cli/config/module.config.php

Large diffs are not rendered by default.

119 changes: 119 additions & 0 deletions module/Cli/src/Command/AbstractOlcsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Dvsa\Olcs\Cli\Command;

use Dvsa\Olcs\Api\Domain\CommandHandlerManager;
use Dvsa\Olcs\Api\Domain\Exception\NotFoundException;
use Olcs\Logging\Log\Logger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

abstract class AbstractOlcsCommand extends Command
{
protected OutputInterface $output;
protected CommandHandlerManager $commandHandlerManager;

public function __construct(
CommandHandlerManager $commandHandlerManager
) {
$this->commandHandlerManager = $commandHandlerManager;
parent::__construct();
}

protected function initializeOutputInterface(OutputInterface $output)
{
$this->output = $output;
}

/**
* Handle an array of command DTOs
*
* @param array $dto
* @return int
*/
protected function handleCommand(array $dto): int
{
try {
foreach ($dto as $count => $dtoCommand) {
$logMessage = "Handling command " . ($count + 1) . ': ' . get_class($dtoCommand);
$this->logAndWriteVerboseMessage($logMessage);

if ($this->isVerbose()) {
$this->output->writeln($logMessage);
}

$result = $this->commandHandlerManager->handleCommand($dtoCommand);
foreach ($result->getMessages() as $message) {
$this->logAndWriteVerboseMessage($message);
}
}
} catch (NotFoundException $e) {
$this->logAndWriteVerboseMessage('NotFoundException: ' . $e->getMessage(), \Laminas\Log\Logger::WARN, true);
return Command::FAILURE;
} catch (\Exception $e) {
$this->logAndWriteVerboseMessage('Error: ' . $e->getMessage(), \Laminas\Log\Logger::ERR, true);
return Command::FAILURE;
}
return Command::SUCCESS;
}

/**
* Log messages, output to console if verbose mode is enabled.
*
* @param string $message
* @param int $logPriority
* @param bool $isError
* @return void
*/
protected function logAndWriteVerboseMessage(string $message, int $logPriority = \Laminas\Log\Logger::DEBUG, bool $isError = false)
{
Logger::log($logPriority, $message);
$formattedMessage = $isError ? "<error>$message</error>" : "<info>$message</info>";

if ($this->output->isVerbose()) {
$this->output->writeln($formattedMessage);
}
}

/**
* Has user requested verbose output
*
* @return bool
*/
protected function isVerbose(): bool
{
return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
}


/**
* Has user requested dry run
*
* @param InputInterface $input
* @return bool
*/
protected function isDryRun(InputInterface $input): bool
{
return $input->getOption('dry-run');
}

/**
* Output the result of an operation with the appropriate message.
*
* @param int $result
* @param string $successMessage
* @param string $failureMessage
* @return int The status code
*/
protected function outputResult(int $result, string $successMessage, string $failureMessage): int
{
if ($result === 0) {
$this->logAndWriteVerboseMessage($successMessage);
return Command::SUCCESS;
} else {
$this->logAndWriteVerboseMessage($failureMessage, \Laminas\Log\Logger::ERR, true);
return Command::FAILURE;
}
}
}
63 changes: 63 additions & 0 deletions module/Cli/src/Command/Batch/AbstractBatchCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Dvsa\Olcs\Cli\Command\Batch;

use Dvsa\Olcs\Api\Domain\CommandHandlerManager;
use Dvsa\Olcs\Api\Domain\Exception\NotFoundException;
use Dvsa\Olcs\Api\Domain\QueryHandler\BundleSerializableInterface;
use Dvsa\Olcs\Api\Domain\QueryHandlerManager;
use Dvsa\Olcs\Cli\Command\AbstractOlcsCommand;
use Dvsa\Olcs\Transfer\Query\QueryInterface;
use Symfony\Component\Console\Input\InputOption;

abstract class AbstractBatchCommand extends AbstractOlcsCommand
{
protected QueryHandlerManager $queryHandlerManager;

public function __construct(
CommandHandlerManager $commandHandlerManager,
QueryHandlerManager $queryHandlerManager
) {
parent::__construct(
$commandHandlerManager
);
$this->queryHandlerManager = $queryHandlerManager;
}

/**
* Add common options to the command
*
* @return void
*/
protected function addCommonOptions(): void
{
$this->addOption(
'dry-run',
'd',
InputOption::VALUE_NONE,
'Perform a dry run without making any changes.'
);
}

/**
* Process a query DTO
*
* @param QueryInterface $dto
* @return BundleSerializableInterface|mixed|null
*/
protected function handleQuery(QueryInterface $dto)
{
try {
$this->logAndWriteVerboseMessage("Handling query: " . get_class($dto));
$result = $this->queryHandlerManager->handleQuery($dto);
return $result;
} catch (NotFoundException $e) {
$this->logAndWriteVerboseMessage("NotFoundException: {$e->getMessage()}", \Laminas\Log\Logger::WARN, true);
} catch (\Exception $e) {
$this->logAndWriteVerboseMessage("Exception: {$e->getMessage()}", \Laminas\Log\Logger::ERR, true);
} catch (\Throwable $e) {
$this->logAndWriteVerboseMessage("Unhandled Error: {$e->getMessage()}", \Laminas\Log\Logger::CRIT, true);
}
return null;
}
}
33 changes: 33 additions & 0 deletions module/Cli/src/Command/Batch/CleanUpAbandonedVariationsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Dvsa\Olcs\Cli\Command\Batch;

use Dvsa\Olcs\Cli\Domain\Command\CleanUpAbandonedVariations;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CleanUpAbandonedVariationsCommand extends AbstractBatchCommand
{
protected static $defaultName = 'batch:clean-up-variations';

protected function configure()
{
$this->setDescription('Clean up abandoned variations.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->initializeOutputInterface($output);

$result = $this->handleCommand([CleanUpAbandonedVariations::create([])]);

if ($result) {
$this->logAndWriteVerboseMessage('<error>Failed to clean up abandoned variations.</error>');
return Command::FAILURE;
}

$this->logAndWriteVerboseMessage('<info>Successfully cleaned up abandoned variations.</info>');
return Command::SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Dvsa\Olcs\Cli\Command\Batch;

use Dvsa\Olcs\Cli\Domain\Command\CompaniesHouseVsOlcsDiffsExport;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class CompaniesHouseVsOlcsDiffsExportCommand extends AbstractBatchCommand
{
protected static $defaultName = 'batch:companies-house-vs-olcs-diffs-export';

protected function configure()
{
$this
->setDescription('Find differences between Companies House and OLCS data and export them.')
->addOption(
'path',
null,
InputOption::VALUE_REQUIRED,
'Path to save the export file.'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->initializeOutputInterface($output);

$path = $input->getOption('path');
if (empty($path)) {
$this->logAndWriteVerboseMessage('<error>No path specified for the export file.</error>');
return Command::FAILURE;
}

$result = $this->handleCommand([CompaniesHouseVsOlcsDiffsExport::create(['path' => $path])]);

return $this->outputResult(
$result,
"Successfully exported differences to {$path}",
'Failed to export differences between Companies House and OLCS data.'
);
}
}
Loading

0 comments on commit aed4a2b

Please sign in to comment.