vendor/ibexa/workflow/src/lib/Event/Subscriber/StartWorkflowSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\Workflow\Event\Subscriber;
  8. use Ibexa\Contracts\Core\Repository\ContentService;
  9. use Ibexa\Contracts\Core\Repository\Events\Content\CreateContentEvent;
  10. use Ibexa\Contracts\Core\Repository\Events\Content\UpdateContentEvent;
  11. use Ibexa\Contracts\Workflow\Registry\WorkflowRegistryInterface;
  12. use Ibexa\Contracts\Workflow\Service\WorkflowServiceInterface;
  13. use Ibexa\Workflow\Exception\NotFoundException;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class StartWorkflowSubscriber implements EventSubscriberInterface
  16. {
  17. /** @var \Ibexa\Contracts\Workflow\Service\WorkflowServiceInterface */
  18. private $workflowService;
  19. /** @var \Ibexa\Contracts\Workflow\Registry\WorkflowRegistryInterface */
  20. private $workflowRegistry;
  21. /** @var \Ibexa\Contracts\Core\Repository\ContentService */
  22. private $contentService;
  23. public function __construct(
  24. WorkflowServiceInterface $workflowService,
  25. WorkflowRegistryInterface $workflowRegistry,
  26. ContentService $contentService
  27. ) {
  28. $this->workflowService = $workflowService;
  29. $this->workflowRegistry = $workflowRegistry;
  30. $this->contentService = $contentService;
  31. }
  32. public static function getSubscribedEvents(): array
  33. {
  34. return [
  35. UpdateContentEvent::class => 'onUpdateContent',
  36. CreateContentEvent::class => 'onCreateContent',
  37. ];
  38. }
  39. public function onUpdateContent(UpdateContentEvent $event): void
  40. {
  41. $content = $event->getContent();
  42. $this->doStartWorkflows((int)$content->id, (int)$content->versionInfo->versionNo);
  43. }
  44. public function onCreateContent(CreateContentEvent $event): void
  45. {
  46. $content = $event->getContent();
  47. $this->doStartWorkflows((int)$content->id, (int)$content->versionInfo->versionNo);
  48. }
  49. private function doStartWorkflows(
  50. int $contentId,
  51. int $versionNo
  52. ): void {
  53. $content = $this->contentService->loadContent($contentId, [], $versionNo);
  54. $supportedWorkflows = $this->workflowRegistry->getSupportedWorkflows($content);
  55. foreach ($supportedWorkflows as $workflow) {
  56. try {
  57. $this->workflowService->loadWorkflowMetadataForContent($content, $workflow->getName());
  58. } catch (NotFoundException $e) {
  59. $this->workflowService->start($content, $workflow->getName());
  60. }
  61. }
  62. }
  63. }
  64. class_alias(StartWorkflowSubscriber::class, 'EzSystems\EzPlatformWorkflow\Event\Subscriber\StartWorkflowSubscriber');