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

AbstractProxyDataEventSubscriber.php

  • user avatar
    Tobias Gross-Vogt authored
    improved handling of proxy api required functions and arguments; authorization: rights and attributes config attributes now optional
    a011f9d7
    History
    AbstractProxyDataEventSubscriber.php 2.83 KiB
    <?php
    
    declare(strict_types=1);
    
    namespace Dbp\Relay\CoreBundle\ProxyApi;
    
    use Dbp\Relay\CoreBundle\Exception\ApiError;
    use Exception;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Symfony\Component\HttpFoundation\Exception\BadRequestException;
    use Symfony\Component\HttpFoundation\Response;
    
    abstract class AbstractProxyDataEventSubscriber implements EventSubscriberInterface
    {
        public static function getSubscribedEvents(): array
        {
            return [
                ProxyDataEvent::NAME.'.'.static::getSubscribedNamespace() => 'onProxyDataEvent',
            ];
        }
    
        /**
         * @throws BadRequestException
         */
        public function onProxyDataEvent(ProxyDataEvent $event): void
        {
            $event->acknowledge();
            $proxyData = $event->getProxyData();
            $functionName = $proxyData->getFunctionName();
            $arguments = $proxyData->getArguments();
            $returnValue = null;
    
            $requiredFunctionArguments = static::getAvailableFunctionSignatures()[$functionName] ?? null;
            if ($requiredFunctionArguments === null) {
                throw new BadRequestException(sprintf('unknown function "%s" under namespace "%s"', $functionName, static::getSubscribedNamespace()));
            } elseif ($this->areAllRequiredArgumentsDefined($requiredFunctionArguments, array_keys($arguments)) === false) {
                throw new BadRequestException(sprintf('incomplete argument list for function "%s" under namespace "%s"', $functionName, static::getSubscribedNamespace()));
            }
    
            try {
                $returnValue = $this->callFunction($functionName, $arguments);
            } catch (Exception $exception) {
                $proxyData->setErrorsFromException($exception);
            }
    
            $proxyData->setData($returnValue);
        }
    
        /**
         * Must be overridden by deriving classes to indicate, which namespace they subscribe for.
         */
        protected static function getSubscribedNamespace(): string
        {
            throw ApiError::withDetails(Response::HTTP_INTERNAL_SERVER_ERROR, 'proxy data event subscribers must subscribe for a namespace');
        }
    
        /**
         * Must be overridden by deriving classes to indicate, which functions are available and which mandatory arguments they have. The format is a follows:
         * ['func1' => ['arg1', 'arg2'], 'func2' => []].
         */
        protected static function getAvailableFunctionSignatures(): array
        {
            throw ApiError::withDetails(Response::HTTP_INTERNAL_SERVER_ERROR, 'proxy data event subscribers must provide a list of available function signatures');
        }
    
        abstract protected function callFunction(string $functionName, array $arguments);
    
        private function areAllRequiredArgumentsDefined(array $requiredFunctionArguments, array $arguments): bool
        {
            return count(array_intersect($requiredFunctionArguments, $arguments)) === count($requiredFunctionArguments);
        }
    }