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

RenderViewHelper::render()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 6
nop 0
dl 0
loc 36
rs 8.439
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\ViewHelpers\Slot;
18
19
use CuyZ\Notiz\Domain\Property\Marker;
20
use CuyZ\Notiz\Exception\DuplicateEntryException;
21
use CuyZ\Notiz\Exception\EntryNotFoundException;
22
use CuyZ\Notiz\Property\Service\MarkerParser;
23
use CuyZ\Notiz\View\Slot\SlotContainer;
24
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
25
26
class RenderViewHelper extends AbstractViewHelper
27
{
28
    const SLOT_CONTAINER = 'SlotContainer';
29
    const SLOT_VALUES = 'SlotValues';
30
    const MARKERS = 'Marker';
31
32
    /**
33
     * @var MarkerParser
34
     */
35
    protected $markerParser;
36
37
    /**
38
     * @param MarkerParser $markerParser
39
     */
40
    public function __construct(MarkerParser $markerParser)
41
    {
42
        $this->markerParser = $markerParser;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function initializeArguments()
49
    {
50
        $this->registerArgument('name', 'string', 'Name of the slot that will be rendered.', true);
51
        $this->registerArgument('markers', 'array', 'Additional markers that will be added to the slot and can be used within the FlexForm.', false, []);
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function render()
58
    {
59
        $result = '';
60
        $name = $this->arguments['name'];
61
        $newMarkers = $this->arguments['markers'];
62
63
        $slotContainer = $this->getSlotContainer();
64
        $slotValues = $this->getSlotValues();
65
        $markers = $this->getMarkers();
66
67
        if (!$slotContainer->has($name)) {
68
            throw EntryNotFoundException::slotNotFound($name);
69
        }
70
71
        foreach ($newMarkers as $key => $value) {
72
            if (isset($markers[$key])) {
73
                throw DuplicateEntryException::markerAlreadyDefined($key, $name);
74
            }
75
        }
76
77
        if (isset($slotValues[$name])) {
78
            foreach ($newMarkers as $key => $value) {
79
                $marker = new Marker($key);
80
                $marker->setValue($value);
81
82
                $markers[$key] = $marker;
83
            }
84
85
            $result = $this->markerParser->replaceMarkers(
86
                $slotValues[$name],
87
                $markers
88
            );
89
        }
90
91
92
        return $result;
93
    }
94
95
    /**
96
     * @return SlotContainer
97
     */
98
    protected function getSlotContainer()
99
    {
100
        return $this->viewHelperVariableContainer->get(__CLASS__, self::SLOT_CONTAINER);
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    protected function getSlotValues()
107
    {
108
        return $this->viewHelperVariableContainer->get(__CLASS__, self::SLOT_VALUES);
109
    }
110
111
    /**
112
     * @return array
113
     */
114
    protected function getMarkers()
115
    {
116
        return $this->viewHelperVariableContainer->get(__CLASS__, self::MARKERS);
117
    }
118
}
119