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..26a81d1d --- /dev/null +++ b/src/Service/ActionService.php @@ -0,0 +1,75 @@ +, 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; + + }//end __construct() + + /** + * 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() + + /** + * 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(); + + 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; + + }//end scanActions() +}//end class