Skip to content
Snippets Groups Projects
Select Git revision
  • b638448e4dfbefa65d757b16f1793a9201260b66
  • 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

AbstractLocalDataEventSubscriber.php

Blame
  • AbstractLocalDataEventSubscriber.php 9.03 KiB
    <?php
    
    declare(strict_types=1);
    
    namespace Dbp\Relay\CoreBundle\LocalData;
    
    use Dbp\Relay\CoreBundle\Authorization\AbstractAuthorizationService;
    use Dbp\Relay\CoreBundle\Exception\ApiError;
    use Symfony\Component\Config\Definition\Builder\NodeDefinition;
    use Symfony\Component\Config\Definition\Builder\TreeBuilder;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Contracts\EventDispatcher\Event;
    
    /*
     * Abstract implementation of a configurable local data provider post event subscriber.
     * It is intended to be derived by local data aware entity post event subscribers.
     * A mapping between source attribute and local data attribute,
     * and default values for the attributes can be specified by means of the deriving event subscriber's bundle config.
     * If no default value is specified, an exception is thrown in the case the mapped source attribute is not found.
     */
    abstract class AbstractLocalDataEventSubscriber extends AbstractAuthorizationService implements EventSubscriberInterface
    {
        protected const ROOT_CONFIG_NODE = 'local_data_mapping';
        protected const SOURCE_ATTRIBUTE_CONFIG_NODE = 'source_attribute';
        protected const LOCAL_DATA_ATTRIBUTE_CONFIG_NODE = 'local_data_attribute';
        protected const AUTHORIZATION_EXPRESSION_CONFIG_NODE = 'authorization_expression';
        protected const ALLOW_LOCAL_QUERY_CONFIG_NODE = 'allow_query';
        protected const DEFAULT_VALUE_ATTRIBUTE_CONFIG_NODE = 'default_value';
        protected const DEFAULT_VALUES_ATTRIBUTE_CONFIG_NODE = 'default_values';
    
        private const SOURCE_ATTRIBUTE_KEY = 'source';
        private const DEFAULT_VALUE_KEY = 'default';
        private const QUERYABLE_KEY = 'queryable';
    
        /*
         * WORKAROUND: could not find a way to determine whether a Symfony config array node was NOT specified since it provides an empty
         * array in case it is not specified. So I use an array value as default which does not seem to be reproducible by the configurator.
         */
        private const ARRAY_VALUE_NOT_SPECIFIED = [null => null];
    
        /** @var array */
        private $attributeMapping;
    
        public function __construct()
        {
            $this->attributeMapping = [];
        }
    
        public function setConfig(array $config)
        {
            $configNode = $config[self::ROOT_CONFIG_NODE] ?? [];
            $rightExpressions = [];
    
            foreach ($configNode as $configMappingEntry) {
                $localDataAttributeName = $configMappingEntry[self::LOCAL_DATA_ATTRIBUTE_CONFIG_NODE];
    
                if (isset($this->attributeMapping[$localDataAttributeName])) {
                    throw new \RuntimeException(sprintf('multiple mapping entries for local data attribute %s', $localDataAttributeName));
                }
    
                $attributeMapEntry = [];
                $attributeMapEntry[self::SOURCE_ATTRIBUTE_KEY] = $configMappingEntry[self::SOURCE_ATTRIBUTE_CONFIG_NODE];
                $attributeMapEntry[self::QUERYABLE_KEY] = $configMappingEntry[self::ALLOW_LOCAL_QUERY_CONFIG_NODE];
    
                $defaultValue = $configMappingEntry[self::DEFAULT_VALUE_ATTRIBUTE_CONFIG_NODE] ?? null;
                if ($defaultValue === null) {
                    $defaultArray = $configMappingEntry[self::DEFAULT_VALUES_ATTRIBUTE_CONFIG_NODE] ?? null;
                    if ($defaultArray !== null && $defaultArray !== self::ARRAY_VALUE_NOT_SPECIFIED) {
                        $defaultValue = $defaultArray;