Completed
Pull Request — master (#121)
by Vladimir
02:51 queued 01:05
created

RedirectSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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