Completed
Push — master ( 7b9015...c5b311 )
by Vladimir
26s queued 11s
created

RedirectSubscriber   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B registerRedirect() 0 29 7
A getSubscribedEvents() 0 6 1
1
<?php
2
3
namespace allejo\stakx\EventSubscriber;
4
5
use allejo\stakx\Document\DynamicPageView;
6
use allejo\stakx\Document\RepeaterPageView;
7
use allejo\stakx\Document\StaticPageView;
8
use allejo\stakx\Event\PageViewAdded;
9
use allejo\stakx\RedirectMapper;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12
class RedirectSubscriber implements EventSubscriberInterface
13
{
14
    /** @var RedirectMapper */
15
    private $redirectMapper;
16
17
    public function __construct(RedirectMapper $redirectMapper)
18
    {
19
        $this->redirectMapper = $redirectMapper;
20
    }
21
22
    public function registerRedirect(PageViewAdded $event)
23
    {
24
        $pageView = $event->getPageView();
25
26
        if ($pageView instanceof StaticPageView || $pageView instanceof DynamicPageView)
27
        {
28
            $redirects = $pageView->getRedirects();
29
30
            foreach ($redirects as $redirect)
31
            {
32
                $this->redirectMapper->registerRedirect($redirect, $pageView->getPermalink());
33
            }
34
        }
35
        elseif ($pageView instanceof RepeaterPageView)
36
        {
37
            $permalinks = $pageView->getRepeaterPermalinks();
38
39
            foreach ($pageView->getRepeaterRedirects() as $repeaterRedirect)
40
            {
41
                foreach ($repeaterRedirect as $index => $redirect)
42
                {
43
                    $this->redirectMapper->registerRedirect(
44
                        $redirect->getEvaluated(),
45
                        $permalinks[$index]->getEvaluated()
46
                    );
47
                }
48
            }
49
        }
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public static function getSubscribedEvents()
56
    {
57
        return [
58
            PageViewAdded::NAME => 'registerRedirect',
59
        ];
60
    }
61
}
62