Completed
Push — master ( b9c485...94fed1 )
by Michał
13:51
created

EventListener/KernelControllerSubscriber.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ResourceBundle\EventListener;
13
14
use Sylius\Bundle\ResourceBundle\Controller\Parameters;
15
use Sylius\Bundle\ResourceBundle\Controller\ParametersParser;
16
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
20
21
/**
22
 * Kernel listener used to set the request on the configurable controllers.
23
 *
24
 * @author Paweł Jędrzejewski <[email protected]>
25
 * @author Arnaud Langade <[email protected]>
26
 * @author Joseph Bielawski <[email protected]>
27
 */
28
class KernelControllerSubscriber implements EventSubscriberInterface
29
{
30
    /**
31
     * @var ParametersParser
32
     */
33
    private $parametersParser;
34
35
    /**
36
     * @var Parameters
37
     */
38
    private $parameters;
39
40
    /**
41
     * @var array
42
     */
43
    private $settings;
44
45
    private $forceApiVersion = false;
46
47
    private $apiVersionHeader = 'Accept';
48
    private $apiGroupsHeader = 'Accept';
49
50
    private $apiVersionRegexp = '/(v|version)=(?P<version>[0-9\.]+)/i';
51
    private $apiGroupsRegexp = '/(g|groups)=(?P<groups>[a-z,_\s]+)/i';
52
53
    public function __construct(ParametersParser $parametersParser, Parameters $parameters, array $settings, $forceApiVersion = false)
54
    {
55
        $this->parametersParser = $parametersParser;
56
        $this->parameters = $parameters;
57
        $this->settings = $settings;
58
        $this->forceApiVersion = $forceApiVersion;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public static function getSubscribedEvents()
65
    {
66
        return [
67
            'kernel.controller' => ['onKernelController', 0],
68
        ];
69
    }
70
71
    /**
72
     * @param FilterControllerEvent $event
73
     */
74
    public function onKernelController(FilterControllerEvent $event)
75
    {
76
        $controller = $event->getController();
77
        if (!is_array($controller)) {
78
            return;
79
        }
80
81
        $controller = reset($controller);
82
        if ($controller instanceof ResourceController) {
83
            $this->processRequest($controller, $event->getRequest());
84
        }
85
    }
86
87
    /**
88
     * @param ResourceController $controller
89
     * @param Request            $request
90
     */
91
    private function processRequest(ResourceController $controller, Request $request)
92
    {
93
        $parameters = array_merge($this->settings, $this->parseApiData($request));
94
        list($parameters, $parameterNames) = $this->parametersParser->parse($parameters, $request);
0 ignored issues
show
The method parse() does not seem to exist on object<Sylius\Bundle\Res...oller\ParametersParser>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
96
        $this->parameters->replace($parameters);
97
        $this->parameters->set('parameter_name', $parameterNames);
98
99
        $controller->getConfiguration()->setRequest($request);
0 ignored issues
show
The method getConfiguration() does not seem to exist on object<Sylius\Bundle\Res...ler\ResourceController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
        $controller->getConfiguration()->setParameters($this->parameters);
0 ignored issues
show
The method getConfiguration() does not seem to exist on object<Sylius\Bundle\Res...ler\ResourceController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
102
        $routeParams = $request->attributes->get('_route_params', []);
103
        if (isset($routeParams['_sylius'])) {
104
            unset($routeParams['_sylius']);
105
106
            $request->attributes->set('_route_params', $routeParams);
107
        }
108
    }
109
110
    /**
111
     * @param Request $request
112
     *
113
     * @return array
114
     */
115
    private function parseApiData(Request $request)
116
    {
117
        $data = [];
118
        if ($request->headers->has($this->apiVersionHeader)) {
119
            if (preg_match($this->apiVersionRegexp, $request->headers->get($this->apiVersionHeader), $matches)) {
120
                $data['serialization_version'] = $matches['version'];
121
            } elseif ($this->forceApiVersion) {
122
                $data['serialization_version'] = '1.0';
123
            }
124
        } elseif ($this->forceApiVersion) {
125
            $data['serialization_version'] = '1.0';
126
        }
127
128
        if ($request->headers->has($this->apiGroupsHeader)) {
129
            if (preg_match($this->apiGroupsRegexp, $request->headers->get($this->apiGroupsHeader), $matches)) {
130
                $data['serialization_groups'] = array_map('trim', explode(',', $matches['groups']));
131
            }
132
        }
133
134
        return array_merge($request->attributes->get('_sylius', []), $data);
135
    }
136
}
137