Completed
Pull Request — master (#118)
by
unknown
02:12
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\DependencyInjection\ContainerInterface;
6
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
7
use Symfony\Component\Yaml\Yaml;
8
9
class ConfigurationListener
10
{
11
    /** @var Sonata\SeoBundle\Seo\SeoPage $sonata */
12
    private $sonata;
13
14
    /** @var Symfony\Component\HttpKernel\Config\FileLocator $kernel */
15
    private $fileLocator;
16
17
    /** @var Symfony\Component\HttpKernel\Log\LoggerInterface $logger */
18
    private $logger;
19
20
    /**
21
     * __construct.
22
     *
23
     * @param Symfony\Component\DependencyInjection\ContainerInterface $container
24
     */
25
    public function __construct(ContainerInterface $container)
26
    {
27
        $this->sonata = $container->get('sonata.seo.page');
28
        $this->logger = $container->get('logger');
29
        $this->fileLocator = $container->get('file_locator');
30
    }
31
32
    /**
33
     * onKernelController.
34
     *
35
     * @param Symfony\Component\HttpKernel\Event\FilterControllerEvent $event
36
     */
37
    public function onKernelController(FilterControllerEvent $event)
38
    {
39
        $controller = $event->getController();
40
41
        // $controller passed can be either a class or a Closure. This is not
42
        // usual in Symfony but it may happen. If it is a class, it comes in
43
        // array format
44
        if (!is_array($controller)) {
45
            return;
46
        }
47
48
        // Get the request with namespace and others information we need and
49
        // construct the bundle namespace
50
        $request = $this->getRequest($event);
51
52
        $namespace = array('_namespace', '_bundle');
53
        foreach ($namespace as $key => $value) {
54
            $namespace[$key] = $request->attributes->get($value);
55
        }
56
57
        array_unshift($namespace, '@');
58
        $namespace = implode('', $namespace);
59
        $namespace = array($namespace, 'Resources', 'config', 'sonata.yml');
60
        $namespace = implode('/', $namespace);
61
62
        // Get the configuration file from the configuration into the
63
        // corresponding bundle
64
        try {
65
            $path = $this->fileLocator->locate($namespace);
66
        } catch (\InvalidArgumentException $e) {
67
            // Nothing to do because there's no SEO file
68
            return;
69
        }
70
71
        try {
72
            $config = Yaml::parse($path);
73
        } 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...
74
            // Do nothing on error. Just log error
75
            $this->logger->err('YAML returns a parse error', array('exception' => $e));
76
            return;
77
        }
78
79
        $controller = $request->attributes->get('_controller');
80
        $action = $request->attributes->get('_action');
81
82
        if (array_key_exists($controller, $config)) {
83
            $config = $config[$controller];
84
85
            if (array_key_exists($action, $config)) {
86
                $config = $config[$action];
87
                $title  = $this->getConfiguration($config, 'title');
88
                $metas  = $this->getConfiguration($config, 'metas');
89
90
                if (!empty($title)) {
91
                    $this->sonata->setTitle($title);
92
                }
93
94
                foreach ($metas as $key => $values) {
95
                    foreach ($values as $head => $value) {
96
                        $this->sonata->addMeta($key, $head, $value);
97
                    }
98
                }
99
            }
100
        }
101
    }
102
103
    /**
104
     * Return the configuration if exists.
105
     *
106
     * @param array  $config
107
     * @param string $key
108
     *
109
     * @return array
110
     */
111
    private function getConfiguration(array $config, $key)
112
    {
113
        if (array_key_exists($key, $config)) {
114
            return $config[$key];
115
        }
116
117
        return array();
118
    }
119
120
    /**
121
     * Return generated request.
122
     *
123
     * @param Symfony\Component\HttpKernel\Event\FilterControllerEvent $event
124
     *
125
     * @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...
126
     */
127
    private function getRequest(FilterControllerEvent $event)
128
    {
129
        $request = $event->getRequest();
130
131
        // Get the controller full namespace in order to get informations
132
        $controller = $request->attributes->get('_controller');
133
        $pattern = '/^(.*)\\\(.*Bundle)\\\Controller\\\(.*Controller)::(.*Action)$/';
134
135
        preg_match($pattern, $controller, $matches);
136
        $request->attributes->set('_namespace', $matches[1]);
137
        $request->attributes->set('_bundle', $matches[2]);
138
        $request->attributes->set('_controller', $matches[3]);
139
        $request->attributes->set('_action', $matches[4]);
140
141
        return $request;
142
    }
143
}
144