Skip to content
Snippets Groups Projects
Select Git revision
  • 0a5bb7dd8eae6767d405855df279ceb1e8236d60
  • main default protected
  • register-logging-channel
  • expr-lang
  • ci-82
  • attr-events
  • locale-wip
  • custom-routes
  • v0.1.85
  • v0.1.84
  • v0.1.83
  • v0.1.82
  • v0.1.81
  • v0.1.80
  • v0.1.79
  • v0.1.78
  • v0.1.77
  • v0.1.76
  • v0.1.75
  • v0.1.74
  • v0.1.73
  • v0.1.72
  • v0.1.71
  • v0.1.70
  • v0.1.69
  • v0.1.68
  • v0.1.67
  • v0.1.65
28 results

AbstractGetAttributeSubscriber.php

  • AbstractGetAttributeSubscriber.php 2.47 KiB
    <?php
    
    declare(strict_types=1);
    
    namespace Dbp\Relay\CoreBundle\Authorization\EventSubscriber;
    
    use Dbp\Relay\CoreBundle\Authorization\Event\GetAttributeEvent;
    use Dbp\Relay\CoreBundle\Authorization\Event\GetAvailableAttributesEvent;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    
    abstract class AbstractGetAttributeSubscriber implements EventSubscriberInterface
    {
        /** @var GetAttributeEvent|null */
        private $event;
    
        public static function getSubscribedEvents(): array
        {
            return [
                GetAvailableAttributesEvent::class => 'onGetAvailableAttributes',
                GetAttributeEvent::class => 'onGetAttributeEvent',
            ];
        }
    
        public function onGetAvailableAttributes(GetAvailableAttributesEvent $event)
        {
            $event->addAttributes($this->getNewAttributes());
        }
    
        public function onGetAttributeEvent(GetAttributeEvent $event)
        {
            try {
                $this->event = $event;
                $attributeName = $event->getAttributeName();
    
                $event->setAttributeValue(in_array($attributeName, $this->getNewAttributes(), true) ?
                    $this->getNewAttributeValue($event->getUserIdentifier(), $attributeName, $event->getAttributeValue()) :
                    $this->updateExistingAttributeValue($event->getUserIdentifier(), $attributeName, $event->getAttributeValue())
                );
            } finally {
                $this->event = null;
            }
        }
    
        /**
         * @param mixed|null $defaultValue
         *
         * @return mixed|null
         */
        public function getAttribute(string $attributeName, $defaultValue = null)
        {
            return $this->event->getAttribute($attributeName, $defaultValue);
        }
    
        /**
         * @param mixed|null $attributeValue The current attribute value
         *
         * @return mixed|null The updated attribute value
         */
        protected function updateExistingAttributeValue(?string $userIdentifier, string $attributeName, $attributeValue)
        {
            return $attributeValue;
        }
    
        /*
         * @return string[] The array of new attribute names that this subscriber provides
         */
        abstract protected function getNewAttributes(): array;
    
        /**
         * @param mixed|null $defaultValue the default value if provided explicitly in the authorization expression, else null
         *
         * @return mixed|null the value for the new attribute with the given name for the given user
         */
        abstract protected function getNewAttributeValue(?string $userIdentifier, string $attributeName, $defaultValue);
    }