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

SlotFlexFormService::getNotificationFlexFormList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
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\Tree\EventGroup\Event\EventDefinition;
20
use CuyZ\Notiz\Service\CacheService;
21
use CuyZ\Notiz\View\Slot\Application\Slot;
22
use CuyZ\Notiz\View\Slot\SlotView;
23
use CuyZ\Notiz\View\ViewPathsAware;
24
use TYPO3\CMS\Core\SingletonInterface;
25
26
class SlotFlexFormService implements SingletonInterface
27
{
28
    /**
29
     * @var SlotViewService
30
     */
31
    protected $slotViewService;
32
33
    /**
34
     * @var CacheService
35
     */
36
    protected $cacheService;
37
38
    /**
39
     * @param SlotViewService $slotViewService
40
     * @param CacheService $cacheService
41
     */
42
    public function __construct(SlotViewService $slotViewService, CacheService $cacheService)
43
    {
44
        $this->slotViewService = $slotViewService;
45
        $this->cacheService = $cacheService;
46
    }
47
48
    /**
49
     * Builds a list of FlexForms, based on the slots of every event.
50
     *
51
     * Events with no slots are ignored.
52
     *
53
     * @param ViewPathsAware $viewPaths
54
     * @return array
55
     */
56
    public function getNotificationFlexFormList(ViewPathsAware $viewPaths)
57
    {
58
        $flexFormList = ['default' => $this->getDefaultFlexForm()];
59
60
        foreach ($this->slotViewService->getEventsWithSlots($viewPaths) as $event => $view) {
61
            $flexForm = $this->getSlotViewFlexForm($view);
62
63
            /** @var EventDefinition $event */
64
            $flexFormList[$event->getFullIdentifier()] = $flexForm;
65
        }
66
67
        return $flexFormList;
68
    }
69
70
    /**
71
     * @param SlotView $view
72
     * @return string
73
     */
74
    public function getSlotViewFlexForm(SlotView $view)
75
    {
76
        $hash = $this->getViewCacheHash($view);
77
78
        if ($this->cacheService->has($hash)) {
79
            $flexForm = $this->cacheService->get($hash);
80
        } else {
81
            $flexForm = $this->buildSlotViewFlexForm($view);
82
            $this->cacheService->set($hash, $flexForm);
83
        }
84
85
        return $flexForm;
86
    }
87
88
    /**
89
     * Builds a complete FlexForm XML, based on the slots of the given event
90
     * view.
91
     *
92
     * For each slot, a text field will be added to the FlexForm.
93
     *
94
     * @param SlotView $view
95
     * @return string
96
     */
97
    protected function buildSlotViewFlexForm(SlotView $view)
98
    {
99
        $slots = $view->getSlots()->getList();
100
101
        if (empty($slots)) {
102
            return null;
103
        }
104
105
        $slotsFlexForm = '';
106
107
        foreach ($slots as $slot) {
108
            $slotsFlexForm .= $this->getSlot($slot);
109
        }
110
111
        $sheet = $this->getSheet('sDEF', $slotsFlexForm);
112
113
        return $this->getBase($sheet);
114
    }
115
116
    /**
117
     * @param string $sheets
118
     * @return string
119
     */
120
    protected function getBase($sheets)
121
    {
122
        return <<<XML
123
<T3DataStructure>
124
    <meta>
125
        <langDisable>1</langDisable>
126
    </meta>
127
    <sheets>
128
        $sheets
129
    </sheets>
130
</T3DataStructure>
131
XML;
132
    }
133
134
    /**
135
     * @param string $identifier
136
     * @param string $slots
137
     * @return string
138
     */
139
    protected function getSheet($identifier, $slots)
140
    {
141
        return <<<XML
142
<$identifier>
143
    <ROOT>
144
        <TCEforms>
145
            <sheetTitle>$identifier</sheetTitle>
146
        </TCEforms>
147
        <type>array</type>
148
        <el>
149
            $slots
150
        </el>
151
    </ROOT>
152
</$identifier>
153
XML;
154
    }
155
156
    /**
157
     * @param Slot $slot
158
     * @return string
159
     */
160
    protected function getSlot(Slot $slot)
161
    {
162
        $label = htmlspecialchars($slot->getLabel());
163
164
        return <<<XML
165
<{$slot->getName()}>
166
    <TCEforms>
167
        <label>$label</label>
168
        <config>
169
            {$slot->getFlexFormConfiguration()}
170
        </config>
171
    </TCEforms>
172
</{$slot->getName()}>
173
XML;
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    protected function getDefaultFlexForm()
180
    {
181
        return <<<XML
182
<T3DataStructure>
183
    <meta>
184
        <langDisable>1</langDisable>
185
    </meta>
186
    <sheets>
187
        <sDEF>
188
            <ROOT>
189
                <TCEforms>
190
                    <sheetTitle>default</sheetTitle>
191
                </TCEforms>
192
                <type>array</type>
193
                <el>
194
                </el>
195
            </ROOT>
196
        </sDEF>
197
    </sheets>
198
</T3DataStructure>
199
XML;
200
    }
201
202
    /**
203
     * @param SlotView $view
204
     * @return string
205
     */
206
    protected function getViewCacheHash(SlotView $view)
207
    {
208
        return 'slots-' . sha1(serialize([
209
                $view->getTemplatePathAndFilename(),
210
                $view->getLayoutRootPaths(),
211
                $view->getPartialRootPaths(),
212
                $view->getEventDefinition()->getFullIdentifier()
213
            ]));
214
    }
215
}
216