Passed
Pull Request — master (#62)
by Romain
03:27
created

build()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 33
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 9
nop 2
dl 0
loc 33
rs 8.439
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\Domain\Event\Form;
18
19
use CuyZ\Notiz\Core\Notification\Notification;
20
use CuyZ\Notiz\Core\Property\Factory\PropertyDefinition;
21
use CuyZ\Notiz\Core\Property\Support\PropertyBuilder;
22
use CuyZ\Notiz\Domain\Property\Email;
23
use TYPO3\CMS\Core\SingletonInterface;
24
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
25
use TYPO3\CMS\Form\Domain\Factory\ArrayFormFactory;
26
use TYPO3\CMS\Form\Domain\Model\FormDefinition;
27
use TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface;
28
use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManagerInterface;
29
30
class DispatchFormNotificationEventPropertyBuilder implements PropertyBuilder, SingletonInterface
31
{
32
    /**
33
     * @var FormPersistenceManagerInterface
34
     */
35
    protected $formPersistenceManager;
36
37
    /**
38
     * @var ArrayFormFactory
39
     */
40
    protected $arrayFormFactory;
41
42
    /**
43
     * @param FormPersistenceManagerInterface $formPersistenceManager
44
     * @param ArrayFormFactory $arrayFormFactory
45
     */
46
    public function __construct(FormPersistenceManagerInterface $formPersistenceManager, ArrayFormFactory $arrayFormFactory)
47
    {
48
        $this->formPersistenceManager = $formPersistenceManager;
49
        $this->arrayFormFactory = $arrayFormFactory;
50
    }
51
52
    /**
53
     * Will fetch all fields from the form definition and add them to the
54
     * definition of the email properties.
55
     *
56
     * This will allow selecting fields that can contain an email address as a
57
     * recipient for email notifications.
58
     *
59
     * @param PropertyDefinition $definition
60
     * @param Notification $notification
61
     * @return void
62
     */
63
    public function build(PropertyDefinition $definition, Notification $notification)
64
    {
65
        if ($definition->getPropertyType() !== Email::class) {
66
            return;
67
        }
68
69
        $eventConfiguration = $notification->getEventConfiguration();
70
71
        // @PHP7
72
        $identifier = isset($eventConfiguration['formDefinition'])
73
            ? $eventConfiguration['formDefinition']
74
            : null;
75
76
        if (!$identifier) {
77
            return;
78
        }
79
80
        if (!$this->formPersistenceManager->exists($identifier)) {
81
            return;
82
        }
83
84
        $formDefinition = $this->getFormDefinition($identifier);
85
86
        /*
87
         * Unfortunately there is no getter method to fetch all elements from a
88
         * form definition, so we use this little hack to get them.
89
         */
90
        $elements = ObjectAccess::getProperty($formDefinition, 'elementsByIdentifier', true);
91
92
        foreach ($elements as $key => $element) {
93
            /** @var FormElementInterface $element */
94
            $definition->addEntry($element->getIdentifier())
95
                ->setLabel($element->getLabel() . ' (' . $element->getIdentifier() . ')');
96
        }
97
    }
98
99
    /**
100
     * @param string $identifier
101
     * @return FormDefinition
102
     */
103
    protected function getFormDefinition($identifier)
104
    {
105
        $formDefinitionArray = $this->formPersistenceManager->load($identifier);
106
107
        return $this->arrayFormFactory->build($formDefinitionArray);
108
    }
109
}
110