Php cs fixes

This commit is contained in:
Julie Lenaerts 2024-08-12 15:52:21 +02:00 committed by Julien Fastré
parent 35199b6993
commit 5d84e997c1
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
5 changed files with 62 additions and 26 deletions

View File

@ -1,12 +1,19 @@
<?php <?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Service\Workflow; namespace Chill\MainBundle\Service\Workflow;
class CancelStaleWorkflow class CancelStaleWorkflow
{ {
public function __construct(public int $workflowId) public function __construct(public int $workflowId) {}
{
}
public function getWorkflowId(): int public function getWorkflowId(): int
{ {

View File

@ -1,11 +1,19 @@
<?php <?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Service\Workflow; namespace Chill\MainBundle\Service\Workflow;
use Chill\MainBundle\Cron\CronJobInterface; use Chill\MainBundle\Cron\CronJobInterface;
use Chill\MainBundle\Entity\CronJobExecution; use Chill\MainBundle\Entity\CronJobExecution;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository; use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use DateInterval;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\Clock\ClockInterface; use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\MessageBusInterface;
@ -18,17 +26,16 @@ class CancelStaleWorkflowCronJob implements CronJobInterface
private const string LAST_CANCELED_WORKFLOW = 'last-canceled-workflow-id'; private const string LAST_CANCELED_WORKFLOW = 'last-canceled-workflow-id';
public function __construct(private readonly EntityWorkflowRepository $workflowRepository, public function __construct(
private readonly ClockInterface $clock, private readonly EntityWorkflowRepository $workflowRepository,
private readonly MessageBusInterface $messageBus, private readonly ClockInterface $clock,
private readonly LoggerInterface $logger, private readonly MessageBusInterface $messageBus,
) private readonly LoggerInterface $logger,
{ ) {}
}
public function canRun(?CronJobExecution $cronJobExecution): bool public function canRun(?CronJobExecution $cronJobExecution): bool
{ {
return $this->clock->now() >= $cronJobExecution->getLastEnd()->add(new DateInterval('P1D')); return $this->clock->now() >= $cronJobExecution->getLastEnd()->add(new \DateInterval('P1D'));
} }
public function getKey(): string public function getKey(): string
@ -40,7 +47,7 @@ class CancelStaleWorkflowCronJob implements CronJobInterface
{ {
$this->logger->info('Cronjob started: Canceling stale workflows.'); $this->logger->info('Cronjob started: Canceling stale workflows.');
$olderThanDate = $this->clock->now()->sub(new DateInterval(self::KEEP_INTERVAL)); $olderThanDate = $this->clock->now()->sub(new \DateInterval(self::KEEP_INTERVAL));
$staleWorkflowIds = $this->workflowRepository->findWorkflowsWithoutFinalStepAndOlderThan($olderThanDate); $staleWorkflowIds = $this->workflowRepository->findWorkflowsWithoutFinalStepAndOlderThan($olderThanDate);
$lastCanceled = self::LAST_CANCELED_WORKFLOW; $lastCanceled = self::LAST_CANCELED_WORKFLOW;
$processedCount = 0; $processedCount = 0;
@ -49,10 +56,10 @@ class CancelStaleWorkflowCronJob implements CronJobInterface
try { try {
$this->messageBus->dispatch(new CancelStaleWorkflow($wId)); $this->messageBus->dispatch(new CancelStaleWorkflow($wId));
$lastCanceled = $wId; $lastCanceled = $wId;
$processedCount++; ++$processedCount;
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->error("Failed to dispatch CancelStaleWorkflow for ID {$wId}", ['exception' => $e]); $this->logger->error("Failed to dispatch CancelStaleWorkflow for ID {$wId}", ['exception' => $e]);
continue; continue;
} }
} }

View File

@ -1,5 +1,14 @@
<?php <?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Service\Workflow; namespace Chill\MainBundle\Service\Workflow;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository; use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
@ -12,9 +21,7 @@ use Symfony\Component\Workflow\Registry;
#[AsMessageHandler] #[AsMessageHandler]
class CancelStaleWorkflowHandler class CancelStaleWorkflowHandler
{ {
public function __construct(private readonly EntityWorkflowRepository $workflowRepository, private readonly Registry $registry, private EntityManagerInterface $em, private LoggerInterface $logger) public function __construct(private readonly EntityWorkflowRepository $workflowRepository, private readonly Registry $registry, private EntityManagerInterface $em, private LoggerInterface $logger) {}
{
}
public function __invoke(CancelStaleWorkflow $message): void public function __invoke(CancelStaleWorkflow $message): void
{ {
@ -28,7 +35,7 @@ class CancelStaleWorkflowHandler
$transitionApplied = false; $transitionApplied = false;
if (count($steps) === 1) { if (1 === count($steps)) {
$this->em->remove($workflow->getCurrentStep()); $this->em->remove($workflow->getCurrentStep());
$this->em->remove($workflow); $this->em->remove($workflow);
} else { } else {
@ -46,12 +53,8 @@ class CancelStaleWorkflowHandler
if (!$transitionApplied) { if (!$transitionApplied) {
$this->logger->error(sprintf('No valid transition found for EntityWorkflow %d.', $workflowId)); $this->logger->error(sprintf('No valid transition found for EntityWorkflow %d.', $workflowId));
throw new UnrecoverableMessageHandlingException( throw new UnrecoverableMessageHandlingException(sprintf('No valid transition found for EntityWorkflow %d.', $workflowId));
sprintf('No valid transition found for EntityWorkflow %d.', $workflowId)
);
} }
} }
} }
} }

View File

@ -23,6 +23,11 @@ use Symfony\Component\Clock\MockClock;
use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\MessageBusInterface;
/**
* @internal
*
* @coversNothing
*/
class CancelStaleWorkflowCronJobTest extends KernelTestCase class CancelStaleWorkflowCronJobTest extends KernelTestCase
{ {
protected function setUp(): void protected function setUp(): void
@ -33,6 +38,7 @@ class CancelStaleWorkflowCronJobTest extends KernelTestCase
/** /**
* @dataProvider buildTestCanRunData * @dataProvider buildTestCanRunData
*
* @throws \Exception * @throws \Exception
*/ */
public function testCanRun(?CronJobExecution $cronJobExecution, bool $expected): void public function testCanRun(?CronJobExecution $cronJobExecution, bool $expected): void

View File

@ -1,5 +1,14 @@
<?php <?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Services\Workflow; namespace Services\Workflow;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow; use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
@ -15,9 +24,13 @@ use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Workflow\Metadata\MetadataStoreInterface; use Symfony\Component\Workflow\Metadata\MetadataStoreInterface;
use Symfony\Component\Workflow\Registry; use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\Transition; use Symfony\Component\Workflow\Transition;
use Symfony\Component\Workflow\Workflow;
use Symfony\Component\Workflow\WorkflowInterface; use Symfony\Component\Workflow\WorkflowInterface;
/**
* @internal
*
* @coversNothing
*/
class CancelStaleWorkflowHandlerTest extends TestCase class CancelStaleWorkflowHandlerTest extends TestCase
{ {
public function testInvokeWorkflowWithOneStep(): void public function testInvokeWorkflowWithOneStep(): void