vendor/pimcore/pimcore/lib/Workflow/EventSubscriber/ChangePublishedStateSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * This source file is available under two different licenses:
  6. * - GNU General Public License version 3 (GPLv3)
  7. * - Pimcore Commercial License (PCL)
  8. * Full copyright and license information is available in
  9. * LICENSE.md which is distributed with this source code.
  10. *
  11. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12. * @license http://www.pimcore.org/license GPLv3 and PCL
  13. */
  14. namespace Pimcore\Workflow\EventSubscriber;
  15. use Pimcore\Model\DataObject\Concrete;
  16. use Pimcore\Model\Document;
  17. use Pimcore\Workflow\Transition;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\Workflow\Event\Event;
  20. /**
  21. * @internal
  22. */
  23. class ChangePublishedStateSubscriber implements EventSubscriberInterface
  24. {
  25. const NO_CHANGE = 'no_change';
  26. const FORCE_PUBLISHED = 'force_published';
  27. const FORCE_UNPUBLISHED = 'force_unpublished';
  28. const SAVE_VERSION = 'save_version';
  29. public function onWorkflowCompleted(Event $event)
  30. {
  31. if (!$this->checkEvent($event)) {
  32. return;
  33. }
  34. /** @var Transition $transition */
  35. $transition = $event->getTransition();
  36. /** @var Document|Concrete $subject */
  37. $subject = $event->getSubject();
  38. $changePublishedState = $transition->getChangePublishedState();
  39. if ($changePublishedState === self::FORCE_UNPUBLISHED) {
  40. $subject->setPublished(false);
  41. } elseif ($changePublishedState === self::FORCE_PUBLISHED) {
  42. $subject->setPublished(true);
  43. }
  44. }
  45. /**
  46. * check's if the event subscriber should be executed
  47. *
  48. * @param Event $event
  49. *
  50. * @return bool
  51. */
  52. private function checkEvent(Event $event): bool
  53. {
  54. return $event->getTransition() instanceof Transition
  55. && ($event->getSubject() instanceof Concrete || $event->getSubject() instanceof Document);
  56. }
  57. public static function getSubscribedEvents(): array
  58. {
  59. return [
  60. 'workflow.completed' => 'onWorkflowCompleted',
  61. ];
  62. }
  63. }