Passed
Pull Request — master (#184)
by Romain
05:11
created

BodySlotsProcessor::doProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
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\Core\Notification\TCA\Processor;
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 BodySlotsProcessor extends GracefulProcessor
34
{
35
    const COLUMN = 'body';
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 string $tableName
60
     */
61
    public function doProcess($tableName)
62
    {
63
        $GLOBALS['TCA'][$tableName]['columns'][self::COLUMN]['displayCond'] = $this->getMailBodyDisplayCond();
64
        $GLOBALS['TCA'][$tableName]['columns'][self::COLUMN]['config']['ds'] = $this->getMailBodyFlexFormList();
65
    }
66
67
    /**
68
     * Builds a condition allowing the mail body to be shown only if the
69
     * selected events does provide slots for the Fluid template.
70
     *
71
     * By default, an event with no custom Fluid template does have a single
72
     * slot.
73
     *
74
     * @return array
75
     */
76
    private function getMailBodyDisplayCond()
77
    {
78
        $eventsWithoutSlots = [];
79
        $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

79
        $events = $this->slotViewService->getEventsWithoutSlots($this->getNotificationSettings()->/** @scrutinizer ignore-call */ getView());
Loading history...
80
81
        foreach ($events as $event => $view) {
82
            /** @var EventDefinition $event */
83
            $eventsWithoutSlots[] = $event->getFullIdentifier();
84
        }
85
86
        return [
87
            'AND' => [
88
                'FIELD:event:!IN:' . implode(',', $eventsWithoutSlots),
89
                'FIELD:event:!=:', // Hide the body when no event is selected.
90
            ],
91
        ];
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    private function getMailBodyFlexFormList()
98
    {
99
        $viewSettings = $this->getNotificationSettings()->getView();
100
101
        return $this->slotFlexFormService->getNotificationFlexFormList($viewSettings);
102
    }
103
104
    /**
105
     * @return EntityEmailSettings|NotificationSettings
106
     */
107
    private function getNotificationSettings()
108
    {
109
        return $this->definitionService
110
            ->getDefinition()
111
            ->getNotification(EntityEmailNotification::getDefinitionIdentifier())
112
            ->getSettings();
113
    }
114
}
115