SeoRequestListener::normalizeSeoOptions()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 6
Ratio 28.57 %

Code Coverage

Tests 4
CRAP Score 9.3088

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 6
loc 21
ccs 4
cts 13
cp 0.3076
rs 9.0534
cc 4
eloc 12
nc 4
nop 1
crap 9.3088
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