Passed
Push — dev ( dc6ac3...67fd32 )
by Romain
06:56
created

SlotViewService::getEventsViews()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (C) 2017
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\View\Slot\Service;
18
19
use CuyZ\Notiz\Definition\DefinitionService;
20
use CuyZ\Notiz\Definition\Tree\EventGroup\Event\EventDefinition;
21
use CuyZ\Notiz\Service\Container;
22
use CuyZ\Notiz\Service\StringService;
23
use CuyZ\Notiz\View\Slot\SlotView;
24
use CuyZ\Notiz\View\ViewPathsAware;
25
use Generator;
26
use TYPO3\CMS\Core\SingletonInterface;
27
use TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException;
28
29
class SlotViewService implements SingletonInterface
30
{
31
    /**
32
     * @var DefinitionService
33
     */
34
    protected $definitionService;
35
36
    /**
37
     * @var StringService
38
     */
39
    protected $stringService;
40
41
    /**
42
     * @param DefinitionService $definitionService
43
     * @param StringService $stringService
44
     */
45
    public function __construct(DefinitionService $definitionService, StringService $stringService)
46
    {
47
        $this->definitionService = $definitionService;
48
        $this->stringService = $stringService;
49
    }
50
51
    /**
52
     * @param EventDefinition $eventDefinition
53
     * @param ViewPathsAware $viewPaths
54
     * @return SlotView
55
     */
56
    public function buildView(EventDefinition $eventDefinition, ViewPathsAware $viewPaths)
57
    {
58
        /** @var SlotView $view */
59
        $view = Container::get(SlotView::class, $eventDefinition);
60
61
        $view->setLayoutRootPaths($viewPaths->getLayoutRootPaths());
62
        $view->setTemplateRootPaths($viewPaths->getTemplateRootPaths());
63
        $view->setPartialRootPaths($viewPaths->getPartialRootPaths());
64
65
        try {
66
            $view->setTemplate($this->getEventTemplatePath($eventDefinition));
67
        } catch (InvalidTemplateResourceException $exception) {
68
            /**
69
             * @deprecated This try/catch block can be removed when TYPO3 v7 is
70
             *             not supported anymore.
71
             */
72
        }
73
74
        if (!$view->hasTemplate()) {
75
            $view->setTemplate('Default');
76
        }
77
78
        return $view;
79
    }
80
81
    /**
82
     * Yields every event from the definition, and their Fluid view that may
83
     * contain slots.
84
     *
85
     * @param ViewPathsAware $viewPaths
86
     * @return Generator
87
     */
88
    public function getEventsViews(ViewPathsAware $viewPaths)
89
    {
90
        if ($this->definitionService->getValidationResult()->hasErrors()) {
91
            return;
92
        }
93
94
        $definition = $this->definitionService->getDefinition();
95
96
        foreach ($definition->getEvents() as $event) {
97
            $view = $this->buildView($event, $viewPaths);
98
99
            yield $event => $view;
100
        }
101
    }
102
103
    /**
104
     * @param ViewPathsAware $viewPaths
105
     * @return Generator
106
     */
107
    public function getEventsWithSlots(ViewPathsAware $viewPaths)
108
    {
109
        foreach ($this->getEventsViews($viewPaths) as $event => $view) {
110
            /** @var SlotView $view */
111
            if (!empty($view->getSlots()->getList())) {
112
                yield $event => $view;
113
            }
114
        }
115
    }
116
117
    /**
118
     * @param ViewPathsAware $viewPaths
119
     * @return Generator
120
     */
121
    public function getEventsWithoutSlots(ViewPathsAware $viewPaths)
122
    {
123
        foreach ($this->getEventsViews($viewPaths) as $event => $view) {
124
            /** @var SlotView $view */
125
            if (empty($view->getSlots()->getList())) {
126
                yield $event => $view;
127
            }
128
        }
129
    }
130
131
    /**
132
     * Returns the calculated template path, based on the identifiers of both
133
     * the dispatched event and its group. The identifiers will be sanitized to
134
     * match the UpperCamelCase format.
135
     *
136
     * For instance, the template path for the event `myEvent` from the group
137
     * `my_company` will be located at `MyCompany/MyEvent.html`.
138
     *
139
     * @param EventDefinition $eventDefinition
140
     * @return string
141
     */
142
    protected function getEventTemplatePath(EventDefinition $eventDefinition)
143
    {
144
        $groupPath = $this->stringService->upperCamelCase($eventDefinition->getGroup()->getIdentifier());
145
        $eventPath = $this->stringService->upperCamelCase($eventDefinition->getIdentifier());
146
147
        return "$groupPath/$eventPath";
148
    }
149
}
150