Completed
Pull Request — master (#119)
by
unknown
02:28
created

ConfigurationListener::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
1
<?php
2
3
namespace Sonata\SeoBundle\EventListener;
4
5
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
6
use Symfony\Component\Yaml\Yaml;
7
8
class ConfigurationListener
9
{
10
    /** @var Sonata\SeoBundle\Seo\SeoPage $sonata */
11
    private $sonata;
12
13
    /** @var Symfony\Component\HttpKernel\Config\FileLocator $kernel */
14
    private $fileLocator;
15
16
    /** @var Symfony\Component\HttpKernel\Log\LoggerInterface $logger */
17
    private $logger;
18
19
    /**
20
     * __construct.
21
     *
22
     * @param Symfony\Component\DependencyInjection\ContainerInterface $container
0 ignored issues
show
Bug introduced by
There is no parameter named $container. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
23
     */
24
    public function __construct(
25
        Symfony\Component\HttpKernel\Config\FileLocator $fileLocator,
26
        Sonata\SeoBundle\Seo\SeoPage $sonata,
27
        Symfony\Component\HttpKernel\Log\LoggerInterface $logger
28
    ) {
29
        $this->sonata = $sonata;
30
        $this->logger = $logger;
31
        $this->fileLocator = $fileLocator;
32
    }
33
34
    /**
35
     * onKernelController.
36
     *
37
     * @param Symfony\Component\HttpKernel\Event\FilterControllerEvent $event
38
     */
39
    public function onKernelController(FilterControllerEvent $event)
40
    {
41
        $controller = $event->getController();
42
43
        // $controller passed can be either a class or a Closure. This is not
44
        // usual in Symfony but it may happen. If it is a class, it comes in
45
        // array format
46
        if (!is_array($controller)) {
47
            return;
48
        }
49
50
        // Get the request with namespace and others information we need and
51
        // construct the bundle namespace
52
        $request = $this->getRequest($event);
53
54
        $namespace = array('_namespace', '_bundle');
55
        foreach ($namespace as $key => $value) {
56
            $namespace[$key] = $request->attributes->get($value);
57
        }
58
59
        array_unshift($namespace, '@');
60
        $namespace = implode('', $namespace);
61
        $namespace = array($namespace, 'Resources', 'config', 'sonata.yml');
62
        $namespace = implode('/', $namespace);
63
64
        // Get the configuration file from the configuration into the
65
        // corresponding bundle
66
        try {
67
            $path = $this->fileLocator->locate($namespace);
68
        } catch (\InvalidArgumentException $e) {
69
            // Nothing to do because there's no SEO file
70
            return;
71
        }
72
73
        try {
74
            $config = Yaml::parse($path);
75
        } catch (Symfony\Component\Yaml\Exception\ParseException $e) {
0 ignored issues
show
Bug introduced by
The class Sonata\SeoBundle\EventLi...xception\ParseException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
76
            // Do nothing on error. Just log error
77
            $this->logger->err('YAML returns a parse error', array('exception' => $e));
78
79
            return;
80
        }
81
82
        $controller = $request->attributes->get('_controller');
83
        $action = $request->attributes->get('_action');
84
85
        if (array_key_exists($controller, $config)) {
86
            $config = $config[$controller];
87
88
            if (array_key_exists($action, $config)) {
89
                $config = $config[$action];
90
                $title  = $this->getConfiguration($config, 'title');
91
                $metas  = $this->getConfiguration($config, 'metas');
92
93
                if (!empty($title)) {
94
                    $this->sonata->setTitle($title);
95
                }
96
97
                foreach ($metas as $key => $values) {
98
                    foreach ($values as $head => $value) {
99
                        $this->sonata->addMeta($key, $head, $value);
100
                    }
101
                }
102
            }
103
        }
104
    }
105
106
    /**
107
     * Return the configuration if exists.
108
     *
109
     * @param array  $config
110
     * @param string $key
111
     *
112
     * @return array
113
     */
114
    private function getConfiguration(array $config, $key)
115
    {
116
        if (array_key_exists($key, $config)) {
117
            return $config[$key];
118
        }
119
120
        return array();
121
    }
122
123
    /**
124
     * Return generated request.
125
     *
126
     * @param Symfony\Component\HttpKernel\Event\FilterControllerEvent $event
127
     *
128
     * @return Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Documentation introduced by
The doc-type Symfony\Component\HttpFoundation\Request; could not be parsed: Expected "|" or "end of type", but got ";" at position 40. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
129
     */
130
    private function getRequest(FilterControllerEvent $event)
131
    {
132
        $request = $event->getRequest();
133
134
        // Get the controller full namespace in order to get informations
135
        $controller = $request->attributes->get('_controller');
136
        $pattern = '/^(.*)\\\(.*Bundle)\\\Controller\\\(.*Controller)::(.*Action)$/';
137
138
        preg_match($pattern, $controller, $matches);
139
        $request->attributes->set('_namespace', $matches[1]);
140
        $request->attributes->set('_bundle', $matches[2]);
141
        $request->attributes->set('_controller', $matches[3]);
142
        $request->attributes->set('_action', $matches[4]);
143
144
        return $request;
145
    }
146
}
147