Completed
Pull Request — master (#118)
by
unknown
02:18
created

ConfigurationListener::getConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
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
77
            return;
78
        }
79
80
        $controller = $request->attributes->get('_controller');
81
        $action = $request->attributes->get('_action');
82
83
        if (array_key_exists($controller, $config)) {
84
            $config = $config[$controller];
85
86
            if (array_key_exists($action, $config)) {
87
                $config = $config[$action];
88
                $title  = $this->getConfiguration($config, 'title');
89
                $metas  = $this->getConfiguration($config, 'metas');
90
91
                if (!empty($title)) {
92
                    $this->sonata->setTitle($title);
93
                }
94
95
                foreach ($metas as $key => $values) {
96
                    foreach ($values as $head => $value) {
97
                        $this->sonata->addMeta($key, $head, $value);
98
                    }
99
                }
100
            }
101
        }
102
    }
103
104
    /**
105
     * Return the configuration if exists.
106
     *
107
     * @param array  $config
108
     * @param string $key
109
     *
110
     * @return array
111
     */
112
    private function getConfiguration(array $config, $key)
113
    {
114
        if (array_key_exists($key, $config)) {
115
            return $config[$key];
116
        }
117
118
        return array();
119
    }
120
121
    /**
122
     * Return generated request.
123
     *
124
     * @param Symfony\Component\HttpKernel\Event\FilterControllerEvent $event
125
     *
126
     * @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...
127
     */
128
    private function getRequest(FilterControllerEvent $event)
129
    {
130
        $request = $event->getRequest();
131
132
        // Get the controller full namespace in order to get informations
133
        $controller = $request->attributes->get('_controller');
134
        $pattern = '/^(.*)\\\(.*Bundle)\\\Controller\\\(.*Controller)::(.*Action)$/';
135
136
        preg_match($pattern, $controller, $matches);
137
        $request->attributes->set('_namespace', $matches[1]);
138
        $request->attributes->set('_bundle', $matches[2]);
139
        $request->attributes->set('_controller', $matches[3]);
140
        $request->attributes->set('_action', $matches[4]);
141
142
        return $request;
143
    }
144
}
145