Passed
Push — master ( 9fba3d...a339bf )
by Romain
03:49
created

BodySlotsProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
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\Notification\Settings\NotificationSettings;
20
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\EntityEmailNotification;
21
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\Settings\EntityEmailSettings;
22
use CuyZ\Notiz\Service\Container;
23
use CuyZ\Notiz\View\Slot\Service\SlotFlexFormService;
24
use CuyZ\Notiz\View\Slot\Service\SlotViewService;
25
26
/**
27
 * Build the TCA array for the dynamic body field of the mail.
28
 *
29
 * The body is a FlexForm field, where definition sheets are handled with
30
 * so-called "slot" that can be registered within the template of the mail.
31
 */
32
class BodySlotsProcessor extends GracefulProcessor
33
{
34
    const COLUMN = 'body';
35
36
    /**
37
     * @var SlotViewService
38
     */
39
    private $slotViewService;
40
41
    /**
42
     * @var SlotFlexFormService
43
     */
44
    private $slotFlexFormService;
45
46
    /**
47
     * Manual dependency injection.
48
     */
49
    public function __construct()
50
    {
51
        parent::__construct();
52
53
        $this->slotViewService = Container::get(SlotViewService::class);
54
        $this->slotFlexFormService = Container::get(SlotFlexFormService::class);
55
    }
56
57
    /**
58
     * @param string $tableName
59
     */
60
    public function doProcess($tableName)
61
    {
62
        $GLOBALS['TCA'][$tableName]['columns'][self::COLUMN]['displayCond'] = $this->getMailBodyDisplayCond();
63
        $GLOBALS['TCA'][$tableName]['columns'][self::COLUMN]['config']['ds'] = $this->getMailBodyFlexFormList();
64
    }
65
66
    /**
67
     * Builds a condition allowing the mail body to be shown only if the
68
     * selected events does provide slots for the Fluid template.
69
     *
70
     * By default, an event with no custom Fluid template does have a single
71
     * slot.
72
     *
73
     * @return array
74
     */
75
    private function getMailBodyDisplayCond()
76
    {
77
        $eventsWithoutSlots = [];
78
        $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

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