Skip to content

Commit

Permalink
Merge pull request #477 from tienvx/dispatch-subject-init-event
Browse files Browse the repository at this point in the history
Dispatch subject init event
  • Loading branch information
tienvx committed May 9, 2020
2 parents 4508b35 + ad2ec81 commit efd61ea
Show file tree
Hide file tree
Showing 28 changed files with 298 additions and 256 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
"ext-json": "*",
"symfony/framework-bundle": "^4.4|^5.0",
"symfony/console": "^4.4|^5.0",
"symfony/workflow": "5.1.0-BETA1",
"symfony/workflow": "^5.1.0-BETA1",
"symfony/validator": "^4.4|^5.0",
"symfony/messenger": "^4.4|^5.0",
"doctrine/doctrine-bundle": "^1.12|^2.0",
"doctrine/orm": "~2.7.0",
"jmgq/a-star": "^1.1"
},
"require-dev": {
"ext-sqlite3": "*",
"phpunit/phpunit": "~8.5.4",
"symfony/yaml": "^4.4|^5.0",
"symfony/expression-language": "^4.4|^5.0",
Expand Down
3 changes: 3 additions & 0 deletions insights.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
// ExampleInsight::class,
\PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterNotSniff::class,
\NunoMaduro\PhpInsights\Domain\Insights\ForbiddenNormalClasses::class,
\NunoMaduro\PhpInsights\Domain\Insights\ForbiddenTraits::class,
\PHP_CodeSniffer\Standards\Generic\Sniffs\Files\OneTraitPerFileSniff::class,
\SlevomatCodingStandard\Sniffs\Classes\SuperfluousTraitNamingSniff::class,
\SlevomatCodingStandard\Sniffs\Classes\SuperfluousInterfaceNamingSniff::class,
\SlevomatCodingStandard\Sniffs\Classes\SuperfluousAbstractClassNamingSniff::class,
],
Expand Down
2 changes: 1 addition & 1 deletion src/Command/WorkflowTryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$workflowName = $input->getArgument('workflow-name');
$subject = $this->subjectManager->createAndSetUp($workflowName, true);
$subject = $this->subjectManager->create($workflowName, true);
$workflow = $this->workflowHelper->get($workflowName);
$generator = $this->generatorManager->get($input->getOption('generator'));
$generatorOptions = new GeneratorOptions();
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Compiler/GuardPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function isModel(ContainerBuilder $container, string $workflowId): boo
return false;
}

protected function replaceDefinition(ContainerBuilder $container, string $guardId)
protected function replaceDefinition(ContainerBuilder $container, string $guardId): void
{
$guardsConfiguration = $container->getDefinition($guardId)->getArgument(0);
$tags = $container->getDefinition($guardId)->getTags();
Expand Down
30 changes: 2 additions & 28 deletions src/Entity/Bug.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Tienvx\Bundle\MbtBundle\Entity;

use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Tienvx\Bundle\MbtBundle\Model\Bug as BugModel;
Expand All @@ -15,6 +14,8 @@
*/
class Bug extends BugModel
{
use TimestampableTrait;

/**
* @ORM\Column(type="integer")
* @ORM\Id()
Expand Down Expand Up @@ -76,35 +77,8 @@ class Bug extends BugModel
*/
protected $messagesCount = 0;

/**
* @ORM\Column(name="created_at", type="datetime", nullable=true)
*/
protected $createdAt;

/**
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
protected $updatedAt;

public function getWorkflow(): WorkflowInterface
{
return new Workflow($this->workflow);
}

/**
* @ORM\PrePersist
*/
public function prePersist(): void
{
$this->createdAt = new DateTime();
$this->updatedAt = new DateTime();
}

/**
* @ORM\PreUpdate
*/
public function preUpdate(): void
{
$this->updatedAt = new DateTime();
}
}
30 changes: 2 additions & 28 deletions src/Entity/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Tienvx\Bundle\MbtBundle\Entity;

use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Tienvx\Bundle\MbtBundle\Model\GeneratorInterface;
Expand All @@ -18,6 +17,8 @@
*/
class Task extends TaskModel
{
use TimestampableTrait;

/**
* @ORM\Column(type="integer")
* @ORM\Id()
Expand Down Expand Up @@ -87,16 +88,6 @@ class Task extends TaskModel
*/
protected $takeScreenshots;

/**
* @ORM\Column(name="created_at", type="datetime", nullable=true)
*/
protected $createdAt;

/**
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
protected $updatedAt;

/**
* @MbtAssert\Reporters
*/
Expand Down Expand Up @@ -124,21 +115,4 @@ public function getReducer(): ReducerInterface
{
return new Reducer($this->reducer);
}

/**
* @ORM\PrePersist
*/
public function prePersist(): void
{
$this->createdAt = new DateTime();
$this->updatedAt = new DateTime();
}

/**
* @ORM\PreUpdate
*/
public function preUpdate(): void
{
$this->updatedAt = new DateTime();
}
}
36 changes: 36 additions & 0 deletions src/Entity/TimestampableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Entity;

use DateTime;
use Doctrine\ORM\Mapping as ORM;

trait TimestampableTrait
{
/**
* @ORM\Column(name="created_at", type="datetime", nullable=true)
*/
protected $createdAt;

/**
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
protected $updatedAt;

/**
* @ORM\PrePersist
*/
public function prePersist(): void
{
$this->createdAt = new DateTime();
$this->updatedAt = new DateTime();
}

/**
* @ORM\PreUpdate
*/
public function preUpdate(): void
{
$this->updatedAt = new DateTime();
}
}
37 changes: 37 additions & 0 deletions src/Event/SubjectInitEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Event;

use Symfony\Contracts\EventDispatcher\Event;
use Tienvx\Bundle\MbtBundle\Model\SubjectInterface;

class SubjectInitEvent extends Event
{
public const NAME = 'mbtbundle.subject.init';

/**
* @var SubjectInterface
*/
protected $subject;

/**
* @var bool
*/
protected $trying;

public function __construct(SubjectInterface $subject, bool $trying = false)
{
$this->subject = $subject;
$this->trying = $trying;
}

public function getSubject(): SubjectInterface
{
return $this->subject;
}

public function isTrying(): bool
{
return $this->trying;
}
}
4 changes: 2 additions & 2 deletions src/EventListener/GuardListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(array $configuration, ExpressionLanguage $expression
$this->expressionLanguage = $expressionLanguage;
}

public function onTransition(GuardEvent $event, string $eventName)
public function onTransition(GuardEvent $event, string $eventName): void
{
if (!isset($this->configuration[$eventName])) {
return;
Expand All @@ -37,7 +37,7 @@ public function onTransition(GuardEvent $event, string $eventName)
}
}

private function validateGuardExpression(GuardEvent $event, string $expression)
private function validateGuardExpression(GuardEvent $event, string $expression): void
{
if (!$this->expressionLanguage->evaluate($expression, ['subject' => $event->getSubject()])) {
$blocker = TransitionBlocker::createBlockedByExpressionGuardListener($expression);
Expand Down
24 changes: 24 additions & 0 deletions src/EventListener/SubjectSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Tienvx\Bundle\MbtBundle\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Tienvx\Bundle\MbtBundle\Event\SubjectInitEvent;
use Tienvx\Bundle\MbtBundle\Model\Subject\SetUpInterface;

class SubjectSubscriber implements EventSubscriberInterface
{
public function onInit(SubjectInitEvent $event): void
{
if ($event->getSubject() instanceof SetUpInterface) {
$event->getSubject()->setUp($event->isTrying());
}
}

public static function getSubscribedEvents()
{
return [
SubjectInitEvent::class => 'onInit',
];
}
}
4 changes: 2 additions & 2 deletions src/EventListener/WorkflowSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(array $places, array $transitions)
$this->transitions = $transitions;
}

public function onEntered(EnteredEvent $event)
public function onEntered(EnteredEvent $event): void
{
$subject = $event->getSubject();
$places = array_keys(array_filter($event->getMarking()->getPlaces()));
Expand All @@ -37,7 +37,7 @@ public function onEntered(EnteredEvent $event)
}
}

public function onTransition(TransitionEvent $event)
public function onTransition(TransitionEvent $event): void
{
$subject = $event->getSubject();
$transition = $event->getTransition()->getName();
Expand Down
12 changes: 6 additions & 6 deletions src/MessageHandler/CaptureScreenshotsMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ public function __construct(
$this->screenshotsCapturer = $screenshotsCapturer;
}

public function setMbtStorage(FilesystemInterface $mbtStorage): void
{
$this->mbtStorage = $mbtStorage;
}

public function __invoke(CaptureScreenshotsMessage $message): void
{
if (!$this->mbtStorage instanceof FilesystemInterface) {
Expand All @@ -76,11 +71,16 @@ public function __invoke(CaptureScreenshotsMessage $message): void
throw new Exception(sprintf('Model checksum of bug with id %d does not match', $bugId));
}

$subject = $this->subjectManager->createAndSetUp($workflow);
$subject = $this->subjectManager->create($workflow);

$this->capture($subject, $bug);
}

public function setMbtStorage(FilesystemInterface $mbtStorage): void
{
$this->mbtStorage = $mbtStorage;
}

protected function capture(SubjectInterface $subject, Bug $bug): void
{
if (!$subject instanceof ScreenshotInterface) {
Expand Down
2 changes: 1 addition & 1 deletion src/MessageHandler/ExecuteTaskMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function __invoke(ExecuteTaskMessage $message): void

protected function execute(Task $task): void
{
$subject = $this->subjectManager->createAndSetUp($task->getWorkflow()->getName());
$subject = $this->subjectManager->create($task->getWorkflow()->getName());
$generator = $this->generatorManager->get($task->getGenerator()->getName());
$workflow = $this->workflowHelper->get($task->getWorkflow()->getName());

Expand Down
10 changes: 5 additions & 5 deletions src/MessageHandler/RemoveScreenshotsMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ public function __construct(SubjectManager $subjectManager)
$this->subjectManager = $subjectManager;
}

public function setMbtStorage(FilesystemInterface $mbtStorage): void
{
$this->mbtStorage = $mbtStorage;
}

public function __invoke(RemoveScreenshotsMessage $message): void
{
if (!$this->mbtStorage instanceof FilesystemInterface) {
Expand All @@ -46,6 +41,11 @@ public function __invoke(RemoveScreenshotsMessage $message): void
$this->removeScreenshots($subject, $bugId);
}

public function setMbtStorage(FilesystemInterface $mbtStorage): void
{
$this->mbtStorage = $mbtStorage;
}

protected function removeScreenshots(SubjectInterface $subject, int $bugId): void
{
if (!$subject instanceof ScreenshotInterface) {
Expand Down
10 changes: 5 additions & 5 deletions src/MessageHandler/ReportBugMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ public function __construct(EntityManagerInterface $entityManager)
$this->entityManager = $entityManager;
}

public function setNotifier(NotifierInterface $notifier): void
{
$this->notifier = $notifier;
}

public function __invoke(ReportBugMessage $message): void
{
if (!$this->notifier instanceof NotifierInterface) {
Expand All @@ -64,6 +59,11 @@ public function __invoke(ReportBugMessage $message): void
$this->sendNotification($bug, $channels);
}

public function setNotifier(NotifierInterface $notifier): void
{
$this->notifier = $notifier;
}

public function setEmailFrom(string $emailFrom): void
{
$this->emailFrom = $emailFrom;
Expand Down
2 changes: 1 addition & 1 deletion src/MessageHandler/TestBugMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function __invoke(TestBugMessage $message): void
$recorded = new Steps();
try {
$workflow = $this->workflowHelper->get($bug->getWorkflow()->getName());
$subject = $this->subjectManager->createAndSetUp($bug->getWorkflow()->getName());
$subject = $this->subjectManager->create($bug->getWorkflow()->getName());

$this->stepsRecorder->record($bug->getSteps(), $workflow, $subject, $recorded);
} catch (Throwable $throwable) {
Expand Down
2 changes: 1 addition & 1 deletion src/MessageHandler/TestPredefinedCaseMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __invoke(TestPredefinedCaseMessage $message): void

$predefinedCase = $this->predefinedCaseManager->get($name);
$workflowName = $predefinedCase->getWorkflow()->getName();
$subject = $this->subjectManager->createAndSetUp($workflowName);
$subject = $this->subjectManager->create($workflowName);

$this->test($predefinedCase, $subject, $workflowName);
}
Expand Down
Loading

0 comments on commit efd61ea

Please sign in to comment.