Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/Command/ActionScanCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace CommonGateway\CoreBundle\Command;

use CommonGateway\CoreBundle\Service\ActionService;
use CommonGateway\CoreBundle\Service\CacheService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* @Author Ruben van der Linde <ruben@conduction.nl>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to add yourself

*
* @license EUPL <https://github.com/ConductionNL/contactcatalogus/blob/master/LICENSE.md>
*
* @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
75 changes: 75 additions & 0 deletions src/Service/ActionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Service to check action validity and run actions.
*
* This service provides a guzzle wrapper to work with sources in the common gateway.
*
* @Author Ruben van der Linde <ruben@conduction.nl>, Robert Zondervan <robert@conduction.nl>, Sarai Misidjan <sarai@conduction.nl>, Barry Brands <barry@conduction.nl>, Wilco Louwerse <wilco@conduction.nl>
*
* @license EUPL <https://github.com/ConductionNL/contactcatalogus/blob/master/LICENSE.md>
*
* @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