custom/plugins/VioCustomerDocs/src/Administration/Subscriber/MediaApiControllerSubscriber.php line 81

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace VioCustomerDocs\Administration\Subscriber;
  4. use Shopware\Core\Framework\Api\Controller\ApiController;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Content\Media\Api\MediaUploadController;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  11. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. class MediaApiControllerSubscriber implements EventSubscriberInterface
  14. {
  15.     private static array $scopedRoutes = [
  16.         'api.action.media.upload',
  17.         'api.media.search',
  18.         'api.media.detail',
  19.         'api.media.delete'
  20.     ];
  21.     private array $arguments;
  22.     /**
  23.      * @var callable
  24.      */
  25.     private $controller;
  26.     /**
  27.      * Returns an array of event names this subscriber wants to listen to.
  28.      *
  29.      * The array keys are event names and the value can be:
  30.      *
  31.      *  * The method name to call (priority defaults to 0)
  32.      *  * An array composed of the method name to call and the priority
  33.      *  * An array of arrays composed of the method names to call and respective
  34.      *    priorities, or 0 if unset
  35.      *
  36.      * For instance:
  37.      *
  38.      *  * ['eventName' => 'methodName']
  39.      *  * ['eventName' => ['methodName', $priority]]
  40.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  41.      *
  42.      * @return array The event names to listen to
  43.      */
  44.     public static function getSubscribedEvents():array
  45.     {
  46.         return [
  47.             KernelEvents::CONTROLLER_ARGUMENTS => 'onControllerArguments',
  48.             KernelEvents::RESPONSE => 'onResponse'
  49.         ];
  50.     }
  51.     /**
  52.      * @param ControllerArgumentsEvent $event
  53.      */
  54.     public function onControllerArguments(ControllerArgumentsEvent $event):void
  55.     {
  56.         if (!in_array($event->getRequest()->attributes->get('_route'), self::$scopedRoutestrue)) {
  57.             return;
  58.         }
  59.         $controller $event->getController();
  60.         if (is_array($controller)) {
  61.             $controller $controller[0];
  62.         }
  63.         if (($controller instanceof MediaUploadController) && strpos($event->getRequest()->get('fileName'), '####CD####') === 0) {
  64.             $this->arguments $event->getArguments();
  65.             $this->controller $event->getController();
  66.         }
  67.         if ($controller instanceof ApiController) {
  68.             $this->arguments $event->getArguments();
  69.             $this->controller $event->getController();
  70.         }
  71.     }
  72.     public function onResponse(ResponseEvent $event): void
  73.     {
  74.         if (!in_array($event->getRequest()->attributes->get('_route'), self::$scopedRoutestrue)) {
  75.             return;
  76.         }
  77.         $result $event->getResponse();
  78.         $controller $this->controller;
  79.         if (is_array($controller)) {
  80.             $controller $controller[0];
  81.         }
  82.         if( $controller instanceof ApiController ) {
  83.             $this->scopeMediaSearch($event$result);
  84.             return;
  85.         }
  86.         if( $controller instanceof MediaUploadController ) {
  87.             if ($result->getStatusCode() !== Response::HTTP_NOT_FOUND) {
  88.                 return;
  89.             }
  90.             if (strpos($event->getRequest()->get('fileName'), '####CD####') === 0) {
  91.                 $this->scopeMediaUpload($event$result);
  92.             }
  93.         }
  94.     }
  95.     /**
  96.      * @param ResponseEvent $event
  97.      * @param Response $result
  98.      * @return void
  99.      */
  100.     private function scopeMediaUpload(ResponseEvent $eventResponse $result): void
  101.     {
  102.         /** @var Context $arguments */
  103.         $context $event->getRequest()->attributes->get('sw-context');
  104.         if (!$context instanceof Context) {
  105.             return;
  106.         }
  107.         $controller $this->controller;
  108.         $arguments $this->arguments;
  109.         $context->scope(Context::SYSTEM_SCOPE, function (Context $context) use (&$result$controller$arguments): void {
  110.             // replace arguments context with scoped context
  111.             /** @noinspection AlterInForeachInspection */
  112.             foreach ($arguments as &$argument) {
  113.                 if ($argument instanceof Context) {
  114.                     $argument $context;
  115.                     break;
  116.                 }
  117.                 if ($argument instanceof Request) {
  118.                     $query $argument->query->all();
  119.                     $query['fileName'] = str_replace('####CD####'substr(md5(uniqid(''true)), 06) . '_'$argument->query->get('fileName'));
  120.                     $argument $argument->duplicate($query);
  121.                     break;
  122.                 }
  123.             }
  124.             /** @var Response $result */
  125.             $result $controller(...$arguments);
  126.         });
  127.         $event->setResponse($result);
  128.     }
  129.     /**
  130.      * @param ResponseEvent $event
  131.      * @param Response $result
  132.      * @return void
  133.      */
  134.     private function scopeMediaSearch(ResponseEvent $eventResponse $result): void
  135.     {
  136.         /** @var Context $arguments */
  137.         $context $event->getRequest()->attributes->get('sw-context');
  138.         if (!$context instanceof Context) {
  139.             return;
  140.         }
  141.         $controller $this->controller;
  142.         $arguments $this->arguments;
  143.         $context->scope(Context::SYSTEM_SCOPE, function (Context $context) use (&$result$controller$arguments): void {
  144.             // replace arguments context with scoped context
  145.             /** @noinspection AlterInForeachInspection */
  146.             foreach ($arguments as &$argument) {
  147.                 if ($argument instanceof Context) {
  148.                     $argument $context;
  149.                     break;
  150.                 }
  151.             }
  152.             /** @var Response $result */
  153.             $result $controller(...$arguments);
  154.         });
  155.         $event->setResponse($result);
  156.     }
  157. }