From 3c42a4c7306a3ceb0264459d6e4bd4312f8a8e5d Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Tue, 19 Dec 2023 11:11:04 +0100 Subject: [PATCH 1/3] Create an action to scan all actions and remove actions without class --- src/Command/ActionScanCommand.php | 71 +++++++++++++++++++++++++++++++ src/Service/ActionService.php | 66 ++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 src/Command/ActionScanCommand.php create mode 100644 src/Service/ActionService.php diff --git a/src/Command/ActionScanCommand.php b/src/Command/ActionScanCommand.php new file mode 100644 index 00000000..ff1acd80 --- /dev/null +++ b/src/Command/ActionScanCommand.php @@ -0,0 +1,71 @@ + + * + * @license EUPL + * + * @category Command + */ +class ActionScanCommand extends Command +{ + + /** + * @var static $defaultName + */ + protected static $defaultName = 'commongateway:actions:scan'; + + /** + * The CacheService + * + * @var ActionService $actionService + */ + private ActionService $actionService; + + /** + * __construct + */ + public function __construct(ActionService $actionService) + { + $this->actionService = $actionService; + parent::__construct(); + + }//end __construct() + + /** + * Configures this commnand. + * + * @return void Nothing. + */ + protected function configure(): void + { + $this + ->setDescription('This command removes actions for which no handler exists') + ->setHelp('This command allows you to run an scanner which deletes actions for which no code is present in the installation of the common gateway (anymore)/'); + + }//end configure() + + /** + * Executes this commnand. + * + * @return int 1 is successfully executed, else 0. + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $this->actionService->setStyle(new SymfonyStyle($input, $output)); + + $this->actionService->scanActions(); + + return Command::SUCCESS; + + }//end execute() +}//end class diff --git a/src/Service/ActionService.php b/src/Service/ActionService.php new file mode 100644 index 00000000..3cb68ade --- /dev/null +++ b/src/Service/ActionService.php @@ -0,0 +1,66 @@ +, Robert Zondervan , Sarai Misidjan , Barry Brands , Wilco Louwerse + * + * @license EUPL + * + * @category Service + */ + +namespace CommonGateway\CoreBundle\Service; + +use App\Entity\Action; +use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\Console\Style\SymfonyStyle; + +class ActionService +{ + private EntityManagerInterface $entityManager; + + private ?SymfonyStyle $style = null; + + public function __construct(EntityManagerInterface $entityManager) + { + $this->entityManager = $entityManager; + } + + /** + * Set symfony style in order to output to the console. + * + * @param SymfonyStyle $style + * + * @return self + */ + public function setStyle(SymfonyStyle $style): self + { + $this->style = $style; + + return $this; + + }//end setStyle() + + public function scanActions(): bool + { + $actions = $this->entityManager->getRepository('App:Action')->findAll(); + + foreach($actions as $action) { + if($action instanceof Action === false) { + continue; + } + + if(class_exists($action->getClass()) === false) { + if($this->style instanceof SymfonyStyle === true) { + $this->style->writeln("Removing {$action->getName()}"); + } + $this->entityManager->remove($action); + $this->entityManager->flush(); + } + } + + return true; + } +} From 4968033da5cb0c6a9a22620fc98f4302b86fd6c0 Mon Sep 17 00:00:00 2001 From: GitHub Actions <> Date: Tue, 19 Dec 2023 10:11:45 +0000 Subject: [PATCH 2/3] Update src from PHP Codesniffer --- src/Service/ActionService.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Service/ActionService.php b/src/Service/ActionService.php index 3cb68ade..abf580c9 100644 --- a/src/Service/ActionService.php +++ b/src/Service/ActionService.php @@ -19,6 +19,7 @@ class ActionService { + private EntityManagerInterface $entityManager; private ?SymfonyStyle $style = null; @@ -26,7 +27,8 @@ class ActionService public function __construct(EntityManagerInterface $entityManager) { $this->entityManager = $entityManager; - } + + }//end __construct() /** * Set symfony style in order to output to the console. @@ -47,20 +49,22 @@ public function scanActions(): bool { $actions = $this->entityManager->getRepository('App:Action')->findAll(); - foreach($actions as $action) { - if($action instanceof Action === false) { + foreach ($actions as $action) { + if ($action instanceof Action === false) { continue; } - if(class_exists($action->getClass()) === false) { - if($this->style instanceof SymfonyStyle === true) { + if (class_exists($action->getClass()) === false) { + if ($this->style instanceof SymfonyStyle === true) { $this->style->writeln("Removing {$action->getName()}"); } + $this->entityManager->remove($action); $this->entityManager->flush(); } } return true; - } -} + + }//end scanActions() +}//end class From e99d23a0c9e9d7f2a860de274f8ebf853b6e4606 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Tue, 19 Dec 2023 13:24:17 +0100 Subject: [PATCH 3/3] Add docblock --- src/Service/ActionService.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Service/ActionService.php b/src/Service/ActionService.php index abf580c9..26a81d1d 100644 --- a/src/Service/ActionService.php +++ b/src/Service/ActionService.php @@ -45,6 +45,11 @@ public function setStyle(SymfonyStyle $style): self }//end setStyle() + /** + * Scans all actions in the database for existing classes, removes the actions for which no class is present anymore. + * + * @return bool True if the scan has progressed successful. + */ public function scanActions(): bool { $actions = $this->entityManager->getRepository('App:Action')->findAll();