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

ConfigurationListener   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 137
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C onKernelController() 0 67 10
A getConfiguration() 0 8 2
A getRequest() 0 16 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\HttpKernel\Config\FileLocator $fileLocator
0 ignored issues
show
Bug introduced by
There is no parameter named $fileLocator. 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...
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 (\Exception $e) {
67
            // Nothing to do because there's no SEO file
68
            return;
69
        }
70
71
        try {
72
            $config = Yaml::parse($path);
73
        } catch (\Exception $e) {
74
            // Do nothing on error. Just log error
75
            $this->logger->err('An error occurred during SEO initialization');
76
            $this->logger->err($e->getMessage());
77
78
            return;
79
        }
80
81
        $controller = $request->attributes->get('_controller');
82
        $action = $request->attributes->get('_action');
83
84
        if (array_key_exists($controller, $config)) {
85
            $config = $config[$controller];
86
87
            if (array_key_exists($action, $config)) {
88
                $config = $config[$action];
89
                $title  = $this->getConfiguration($config, 'title');
90
                $metas  = $this->getConfiguration($config, 'metas');
91
92
                if (!empty($title)) {
93
                    $this->sonata->setTitle($title);
94
                }
95
96
                foreach ($metas as $key => $values) {
97
                    foreach ($values as $head => $value) {
98
                        $this->sonata->addMeta($key, $head, $value);
99
                    }
100
                }
101
            }
102
        }
103
    }
104
105
    /**
106
     * Return the configuration if exists.
107
     *
108
     * @param array  $config
109
     * @param string $key
110
     *
111
     * @return array
112
     */
113
    private function getConfiguration(array $config, $key)
114
    {
115
        if (array_key_exists($key, $config)) {
116
            return $config[$key];
117
        }
118
119
        return array();
120
    }
121
122
    /**
123
     * Return generated request.
124
     *
125
     * @param Symfony\Component\HttpKernel\Event\FilterControllerEvent $event
126
     *
127
     * @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...
128
     */
129
    private function getRequest(FilterControllerEvent $event)
130
    {
131
        $request = $event->getRequest();
132
133
        // Get the controller full namespace in order to get informations
134
        $controller = $request->attributes->get('_controller');
135
        $pattern = '/^(.*)\\\(.*Bundle)\\\Controller\\\(.*Controller)::(.*Action)$/';
136
137
        preg_match($pattern, $controller, $matches);
138
        $request->attributes->set('_namespace', $matches[1]);
139
        $request->attributes->set('_bundle', $matches[2]);
140
        $request->attributes->set('_controller', $matches[3]);
141
        $request->attributes->set('_action', $matches[4]);
142
143
        return $request;
144
    }
145
}
146