Passed
Pull Request — master (#148)
by Romain
07:54 queued 03:12
created

BodySlotsProvider::getMailBodyFlexFormList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (C) 2018
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\Backend\FormEngine\DataProvider;
18
19
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition;
20
use CuyZ\Notiz\Core\Notification\Settings\NotificationSettings;
21
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\EntityEmailNotification;
22
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\Settings\EntityEmailSettings;
23
use CuyZ\Notiz\Service\Container;
24
use CuyZ\Notiz\View\Slot\Service\SlotFlexFormService;
25
use CuyZ\Notiz\View\Slot\Service\SlotViewService;
26
27
/**
28
 * Build the TCA array for the dynamic body field of the mail.
29
 *
30
 * The body is a FlexForm field, where definition sheets are handled with
31
 * so-called "slot" that can be registered within the template of the mail.
32
 */
33
class BodySlotsProvider extends GracefulProvider
34
{
35
    const COLUMN = '__mailBody';
36
37
    /**
38
     * @var SlotViewService
39
     */
40
    private $slotViewService;
41
42
    /**
43
     * @var SlotFlexFormService
44
     */
45
    private $slotFlexFormService;
46
47
    /**
48
     * Manual dependency injection.
49
     */
50
    public function __construct()
51
    {
52
        parent::__construct();
53
54
        $this->slotViewService = Container::get(SlotViewService::class);
55
        $this->slotFlexFormService = Container::get(SlotFlexFormService::class);
56
    }
57
58
    /**
59
     * @param array $result
60
     * @return array
61
     */
62
    public function process(array $result)
63
    {
64
        $tableName = $result['tableName'];
65
66
        if (!isset($GLOBALS['TCA'][$tableName]['ctrl'][self::COLUMN])) {
67
            return $result;
68
        }
69
70
        $columnName = $GLOBALS['TCA'][$tableName]['ctrl'][self::COLUMN];
71
72
        if (!isset($GLOBALS['TCA'][$tableName]['columns'][$columnName])) {
73
            return $result;
74
        }
75
76
        $GLOBALS['TCA'][$tableName]['columns'][$columnName]['displayCond'] = $this->getMailBodyDisplayCond();
77
        $GLOBALS['TCA'][$tableName]['columns'][$columnName]['config']['ds'] = $this->getMailBodyFlexFormList();
78
79
        return $result;
80
    }
81
82
    /**
83
     * Builds a condition allowing the mail body to be shown only if the
84
     * selected events does provide slots for the Fluid template.
85
     *
86
     * By default, an event with no custom Fluid template does have a single
87
     * slot.
88
     *
89
     * @return array
90
     */
91
    private function getMailBodyDisplayCond()
92
    {
93
        $eventsWithoutSlots = [];
94
        $events = $this->slotViewService->getEventsWithoutSlots($this->getNotificationSettings()->getView());
0 ignored issues
show
Bug introduced by
The method getView() does not exist on CuyZ\Notiz\Core\Notifica...gs\NotificationSettings. It seems like you code against a sub-type of said class. However, the method does not exist in CuyZ\Notiz\Core\Notifica...ptyNotificationSettings. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
        $events = $this->slotViewService->getEventsWithoutSlots($this->getNotificationSettings()->/** @scrutinizer ignore-call */ getView());
Loading history...
95
96
        foreach ($events as $event => $view) {
97
            /** @var EventDefinition $event */
98
            $eventsWithoutSlots[] = $event->getFullIdentifier();
99
        }
100
101
        return [
102
            'AND' => [
103
                'FIELD:event:!IN:' . implode(',', $eventsWithoutSlots),
104
                'FIELD:event:!=:', // Hide the body when no event is selected.
105
            ],
106
        ];
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    private function getMailBodyFlexFormList()
113
    {
114
        $viewSettings = $this->getNotificationSettings()->getView();
115
116
        return $this->slotFlexFormService->getNotificationFlexFormList($viewSettings);
117
    }
118
119
    /**
120
     * @return EntityEmailSettings|NotificationSettings
121
     */
122
    private function getNotificationSettings()
123
    {
124
        return $this->definitionService
125
            ->getDefinition()
126
            ->getNotification(EntityEmailNotification::getDefinitionIdentifier())
127
            ->getSettings();
128
    }
129
}
130