vendor/ezsystems/repository-forms/lib/Form/Processor/RoleFormProcessor.php line 51

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the eZ RepositoryForms package.
  4.  *
  5.  * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  7.  */
  8. namespace EzSystems\RepositoryForms\Form\Processor;
  9. use eZ\Publish\API\Repository\RoleService;
  10. use EzSystems\RepositoryForms\Event\FormActionEvent;
  11. use EzSystems\RepositoryForms\Event\RepositoryFormEvents;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class RoleFormProcessor implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var RoleService
  17.      */
  18.     private $roleService;
  19.     public function __construct(RoleService $roleService)
  20.     {
  21.         $this->roleService $roleService;
  22.     }
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             RepositoryFormEvents::ROLE_UPDATE => ['processDefaultAction'],
  27.             RepositoryFormEvents::ROLE_SAVE => ['processSaveRole'],
  28.             RepositoryFormEvents::ROLE_REMOVE_DRAFT => ['processRemoveDraft'],
  29.         ];
  30.     }
  31.     public function processDefaultAction(FormActionEvent $event)
  32.     {
  33.         if ($this->isDefaultEvent($event)) {
  34.             $this->processSaveRole($event);
  35.         }
  36.     }
  37.     public function processSaveRole(FormActionEvent $event)
  38.     {
  39.         $roleDraft $this->getRoleDraft($event);
  40.         $this->roleService->updateRoleDraft($roleDraft$this->getRoleData($event));
  41.         $this->roleService->publishRoleDraft($roleDraft);
  42.     }
  43.     public function processRemoveDraft(FormActionEvent $event)
  44.     {
  45.         $this->roleService->deleteRoleDraft($this->getRoleDraft($event));
  46.     }
  47.     /**
  48.      * Returns true if the event is the default event, meaning Enter has been pressed in the form.
  49.      *
  50.      * There's no need to process the default action (save) for explicit events (save, cancel).
  51.      * Saving is not needed when cancelling, and when the Save button is clicked the save action takes care of it.
  52.      * The default action is only needed when the form has been submitted by pressing Enter.
  53.      *
  54.      * @param FormActionEvent $event
  55.      *
  56.      * @return bool
  57.      */
  58.     protected function isDefaultEvent(FormActionEvent $event)
  59.     {
  60.         return $event->getClickedButton() === null;
  61.     }
  62.     /**
  63.      * Returns the role data for the event.
  64.      *
  65.      * @param FormActionEvent $event
  66.      *
  67.      * @return \EzSystems\RepositoryForms\Data\Role\RoleData
  68.      */
  69.     protected function getRoleData(FormActionEvent $event)
  70.     {
  71.         return $event->getData();
  72.     }
  73.     /**
  74.      * Returns the role draft for the event.
  75.      *
  76.      * @param FormActionEvent $event
  77.      *
  78.      * @return \eZ\Publish\API\Repository\Values\User\RoleDraft
  79.      */
  80.     protected function getRoleDraft(FormActionEvent $event)
  81.     {
  82.         return $this->getRoleData($event)->roleDraft;
  83.     }
  84. }