SeoRequestListener   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 6.38 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 70%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 6
dl 6
loc 94
ccs 28
cts 40
cp 0.7
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C onMasterRequest() 0 40 7
A normalizeSeoOptions() 6 21 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Bankiru\Seo\Listener;
4
5
use Bankiru\Seo\Exception\MatchingException;
6
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
7
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
10
final class SeoRequestListener
11
{
12
    /**
13
     * @var SeoRequestInterface
14
     */
15
    private $seoRequest;
16
17
    /**
18
     * SeoRequestListener constructor.
19
     *
20
     * @param SeoRequestInterface $seoRequest
21
     */
22 3
    public function __construct(SeoRequestInterface $seoRequest)
23
    {
24
25 3
        $this->seoRequest = $seoRequest;
26 3
    }
27
28
    /**
29
     * @param FilterControllerEvent $event
30
     *
31
     * @throws \UnexpectedValueException
32
     * @throws NotFoundHttpException
33
     * @throws \LogicException
34
     */
35 3
    public function onMasterRequest(FilterControllerEvent $event)
36
    {
37 3
        if (!$event->isMasterRequest()) {
38
            // do not resolve seo on subrequests
39
            return;
40
        }
41
42 3
        $controller = $event->getController();
43 3
        if (is_array($controller) && $controller[0] instanceof RedirectController) {
44
            // do not resolve seo on redirects
45
            return;
46
        }
47
48 3
        $request = $event->getRequest();
49
50 3
        $options    = $request->attributes->get('_seo_options', false);
51 3
        $seoOptions = $this->normalizeSeoOptions($options);
52 3
        if (true !== $seoOptions['enabled']) {
53 1
            return;
54
        }
55
56 2
        $this->seoRequest->setDestination(
57 2
            RequestDestinationFactory::createFromRequest(
58 2
                $request,
59 2
                $seoOptions['destination']
60 2
            )
61 2
        );
62
63 2
        if (false === $seoOptions['match']) {
64
            return;
65
        }
66
67
        try {
68 2
            $page = $this->seoRequest->getPage();
69 2
        } catch (MatchingException $exception) {
70 1
            throw new NotFoundHttpException(null, $exception);
71
        }
72
73 1
        $request->attributes->set('_seo_page', $page);
74 1
    }
75
76
    /**
77
     * @param mixed $options
78
     *
79
     * @return array
80
     * @throws \UnexpectedValueException
81
     */
82 3
    private function normalizeSeoOptions($options)
83
    {
84 3 View Code Duplication
        if (null === $options) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
            return ['enabled' => false, 'match' => false, 'destination' => []];
86
        }
87 3 View Code Duplication
        if (is_bool($options)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88 3
            return ['enabled' => $options, 'match' => true, 'destination' => []];
89
        }
90
        if (!is_array($options)) {
91
            throw new \UnexpectedValueException('Seo options should be boolean, null or array');
92
        }
93
94
        return array_replace_recursive(
95
            [
96
                'enabled'     => true,
97
                'match'       => true,
98
                'destination' => [],
99
            ],
100
            $options
101
        );
102
    }
103
}
104