Passed
Push — dev ( 30f77e...4b2c2f )
by Romain
03:39
created

EntityEmailTemplateBuilder::getTemplatePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
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\Domain\Notification\Email\Application\EntityEmail\Service;
18
19
use CuyZ\Notiz\Channel\Payload;
20
use CuyZ\Notiz\Definition\DefinitionService;
21
use CuyZ\Notiz\Definition\Tree\Definition;
22
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\EntityEmailNotification;
23
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\Settings\EntityEmailSettings;
24
use CuyZ\Notiz\Domain\Property\Marker;
25
use CuyZ\Notiz\Event\Event;
26
use CuyZ\Notiz\Property\Service\MarkerParser;
27
use CuyZ\Notiz\Service\StringService;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
use TYPO3\CMS\Fluid\View\StandaloneView;
30
31
class EntityEmailTemplateBuilder
32
{
33
    /**
34
     * @var EntityEmailNotification
35
     */
36
    protected $notification;
37
38
    /**
39
     * @var EntityEmailSettings
40
     */
41
    protected $notificationSettings;
42
43
    /**
44
     * @var Event
45
     */
46
    protected $event;
47
48
    /**
49
     * @var Definition
50
     */
51
    protected $definition;
52
53
    /**
54
     * @var MarkerParser
55
     */
56
    protected $markerParser;
57
58
    /**
59
     * @var StringService
60
     */
61
    protected $stringService;
62
63
    /**
64
     * @var Marker[]
65
     */
66
    protected $markers = [];
67
68
    /**
69
     * @param Payload $payload
70
     * @param DefinitionService $definitionService
71
     * @param MarkerParser $markerParser
72
     * @param StringService $stringService
73
     */
74
    public function __construct(Payload $payload, DefinitionService $definitionService, MarkerParser $markerParser, StringService $stringService)
75
    {
76
        $this->notification = $payload->getNotification();
77
        $this->notificationSettings = $payload->getNotificationDefinition()->getSettings();
78
79
        $this->event = $payload->getEvent();
80
81
        $this->definition = $definitionService->getDefinition();
82
        $this->markerParser = $markerParser;
83
        $this->stringService = $stringService;
84
85
        $this->markers = $payload->getEvent()->getProperties(Marker::class);
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getSubject()
92
    {
93
        return $this->markerParser->replaceMarkers(
94
            $this->notification->getSubject(),
95
            $this->markers
96
        );
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function getBody()
103
    {
104
        /** @var StandaloneView $view */
105
        $view = GeneralUtility::makeInstance(StandaloneView::class);
106
107
        $viewSettings = $this->notificationSettings->getView();
108
109
        $view->setLayoutRootPaths($viewSettings->getLayoutRootPaths());
110
        $view->setTemplateRootPaths($viewSettings->getTemplateRootPaths());
111
        $view->setPartialRootPaths($viewSettings->getPartialRootPaths());
112
113
        $view->setTemplate($this->getTemplatePath());
114
        
115
        if (!$view->hasTemplate()) {
116
            $view->setTemplate('Default');
117
        }
118
119
        $layout = $viewSettings->getLayout($this->notification->getLayout());
120
121
        $body = $this->markerParser->replaceMarkers(
122
            $this->notification->getBody(),
123
            $this->markers
124
        );
125
126
        $view->assign('body', $body);
127
        $view->assign('layout', $layout->getPath());
128
        $view->assign('markers', $this->markers);
129
130
        return $view->render();
131
    }
132
133
    /**
134
     * Returns the calculated template path, based on the identifiers of both
135
     * the dispatched event and its group. The identifiers will be sanitized to
136
     * match the UpperCamelCase format.
137
     *
138
     * For instance, the template path for the event `myEvent` from the group
139
     * `my_company` will be located at `MyCompany/MyEvent.html`.
140
     *
141
     * @return string
142
     */
143
    protected function getTemplatePath()
144
    {
145
        $eventDefinition = $this->event->getDefinition();
146
147
        $groupPath = $this->stringService->upperCamelCase($eventDefinition->getGroup()->getIdentifier());
148
        $eventPath = $this->stringService->upperCamelCase($eventDefinition->getIdentifier());
149
150
        return "$groupPath/$eventPath";
151
    }
152
}
153