SlotViewService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * Copyright (C)
6
 * Nathan Boiron <[email protected]>
7
 * Romain Canon <[email protected]>
8
 *
9
 * This file is part of the TYPO3 NotiZ project.
10
 * It is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License, either
12
 * version 3 of the License, or any later version.
13
 *
14
 * For the full copyright and license information, see:
15
 * http://www.gnu.org/licenses/gpl-3.0.html
16
 */
17
18
namespace CuyZ\Notiz\View\Slot\Service;
19
20
use CuyZ\Notiz\Core\Definition\DefinitionService;
21
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition;
22
use CuyZ\Notiz\Service\Container;
23
use CuyZ\Notiz\Service\StringService;
24
use CuyZ\Notiz\View\Slot\SlotView;
25
use CuyZ\Notiz\View\ViewPathsAware;
26
use Generator;
27
use TYPO3\CMS\Core\SingletonInterface;
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): SlotView
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
        $view->setTemplate($this->getEventTemplatePath($eventDefinition));
65
66
        if (!$view->hasTemplate()) {
67
            $view->setTemplate('Default');
68
        }
69
70
        return $view;
71
    }
72
73
    /**
74
     * Yields every event from the definition, and their Fluid view that may
75
     * contain slots.
76
     *
77
     * @param ViewPathsAware $viewPaths
78
     * @return Generator
79
     */
80
    public function getEventsViews(ViewPathsAware $viewPaths): Generator
81
    {
82
        if ($this->definitionService->getValidationResult()->hasErrors()) {
83
            return;
84
        }
85
86
        $definition = $this->definitionService->getDefinition();
87
88
        foreach ($definition->getEvents() as $event) {
89
            $view = $this->buildView($event, $viewPaths);
90
91
            yield $event => $view;
92
        }
93
    }
94
95
    /**
96
     * @param ViewPathsAware $viewPaths
97
     * @return Generator
98
     */
99
    public function getEventsWithSlots(ViewPathsAware $viewPaths): Generator
100
    {
101
        foreach ($this->getEventsViews($viewPaths) as $event => $view) {
102
            /** @var SlotView $view */
103
            if (!empty($view->getSlots()->getList())) {
104
                yield $event => $view;
105
            }
106
        }
107
    }
108
109
    /**
110
     * @param ViewPathsAware $viewPaths
111
     * @return Generator|SlotView[]
112
     */
113
    public function getEventsWithoutSlots(ViewPathsAware $viewPaths): Generator
114
    {
115
        foreach ($this->getEventsViews($viewPaths) as $event => $view) {
116
            /** @var SlotView $view */
117
            if (empty($view->getSlots()->getList())) {
118
                yield $event => $view;
119
            }
120
        }
121
    }
122
123
    /**
124
     * Returns the calculated template path, based on the identifiers of both
125
     * the dispatched event and its group. The identifiers will be sanitized to
126
     * match the UpperCamelCase format.
127
     *
128
     * For instance, the template path for the event `myEvent` from the group
129
     * `my_company` will be located at `MyCompany/MyEvent.html`.
130
     *
131
     * @param EventDefinition $eventDefinition
132
     * @return string
133
     */
134
    protected function getEventTemplatePath(EventDefinition $eventDefinition): string
135
    {
136
        $groupPath = $this->stringService->upperCamelCase($eventDefinition->getGroup()->getIdentifier());
137
        $eventPath = $this->stringService->upperCamelCase($eventDefinition->getIdentifier());
138
139
        return "$groupPath/$eventPath";
140
    }
141
}
142