Completed
Pull Request — 1.5 (#769)
by Paweł
19:46 queued 09:38
created

ContextDataCollector::getRouteData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Template Engine Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\TemplatesSystemBundle\DataCollector;
16
17
use SWP\Bundle\ContentBundle\Model\RouteInterface;
18
use SWP\Component\TemplatesSystem\Gimme\Context\Context;
19
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaInterface;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
23
24
class ContextDataCollector extends DataCollector
25
{
26
    /**
27
     * @var Context
28
     */
29
    protected $context;
30
31
    /**
32
     * ContextDataCollector constructor.
33
     *
34
     * @param Context $context
35
     */
36
    public function __construct(Context $context)
37
    {
38
        $this->context = $context;
39
    }
40
41
    /**
42
     * @param Request         $request
43
     * @param Response        $response
44
     * @param \Exception|null $exception
45
     */
46
    public function collect(Request $request, Response $response, \Exception $exception = null)
47
    {
48
        $this->data = [
49
            'currentPage' => $this->context->getCurrentPage() instanceof MetaInterface ?
50
                $this->getRouteData($this->context->getCurrentPage()->getValues()) :
51
                [],
52
            'registeredMeta' => $this->context->getRegisteredMeta(),
53
        ];
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public function getData()
60
    {
61
        return $this->data;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getName()
68
    {
69
        return 'context_collector';
70
    }
71
72
    /**
73
     * We don't have anything to reset here.
74
     */
75
    public function reset()
76
    {
77
        return;
78
    }
79
80
    private function getRouteData(?RouteInterface $route)
81
    {
82
        if (null === $route) {
83
            return null;
84
        }
85
86
        return [
87
            'title' => $route->getName(),
88
            'parent' => $this->getRouteData($route->getParent()),
89
            'templateName' => $route->getTemplateName(),
90
            'articlesTemplateName' => $route->getArticlesTemplateName(),
91
            'type' => $route->getType(),
92
            'cacheTimeInSeconds' => $route->getCacheTimeInSeconds(),
93
            'slug' => $route->getSlug(),
94
            'variablePattern' => $route->getVariablePattern(),
95
            'staticPrefix' => $route->getStaticPrefix(),
96
            'position' => $route->getPosition(),
97
        ];
98
    }
99
}
100