DispatchFormNotificationEventPropertyBuilder   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 87
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 32 5
A getFormDefinition() 0 5 1
A __construct() 0 8 1
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * Copyright (C)
6
 * Nathan Boiron <[email protected]>
7
 * Romain Canon <[email protected]>
8
 *
9
 * This file is part of the TYPO3 NotiZ project.
10
 * It is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License, either
12
 * version 3 of the License, or any later version.
13
 *
14
 * For the full copyright and license information, see:
15
 * http://www.gnu.org/licenses/gpl-3.0.html
16
 */
17
18
namespace CuyZ\Notiz\Domain\Event\Form;
19
20
use CuyZ\Notiz\Core\Notification\Notification;
21
use CuyZ\Notiz\Core\Property\Builder\PropertyBuilder;
22
use CuyZ\Notiz\Core\Property\Factory\PropertyDefinition;
23
use CuyZ\Notiz\Core\Property\Service\TagsPropertyService;
24
use CuyZ\Notiz\Domain\Property\Email;
25
use TYPO3\CMS\Core\SingletonInterface;
26
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Reflection\ObjectAccess was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use TYPO3\CMS\Form\Domain\Factory\ArrayFormFactory;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Form\Domain\Factory\ArrayFormFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use TYPO3\CMS\Form\Domain\Model\FormDefinition;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Form\Domain\Model\FormDefinition was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Form\Domain\Mo...ts\FormElementInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManagerInterface;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Form\Mvc\Persi...istenceManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
32
class DispatchFormNotificationEventPropertyBuilder implements PropertyBuilder, SingletonInterface
33
{
34
    /**
35
     * @var TagsPropertyService
36
     */
37
    protected $tagsPropertyService;
38
39
    /**
40
     * @var FormPersistenceManagerInterface
41
     */
42
    protected $formPersistenceManager;
43
44
    /**
45
     * @var ArrayFormFactory
46
     */
47
    protected $arrayFormFactory;
48
49
    /**
50
     * @param TagsPropertyService $tagsPropertyService
51
     * @param FormPersistenceManagerInterface $formPersistenceManager
52
     * @param ArrayFormFactory $arrayFormFactory
53
     */
54
    public function __construct(
55
        TagsPropertyService $tagsPropertyService,
56
        FormPersistenceManagerInterface $formPersistenceManager,
57
        ArrayFormFactory $arrayFormFactory
58
    ) {
59
        $this->tagsPropertyService = $tagsPropertyService;
60
        $this->formPersistenceManager = $formPersistenceManager;
61
        $this->arrayFormFactory = $arrayFormFactory;
62
    }
63
64
    /**
65
     * Will fetch all fields from the form definition and add them to the
66
     * definition of the email properties.
67
     *
68
     * This will allow selecting fields that can contain an email address as a
69
     * recipient for email notifications.
70
     *
71
     * @param PropertyDefinition $definition
72
     * @param Notification $notification
73
     * @return void
74
     */
75
    public function build(PropertyDefinition $definition, Notification $notification)
76
    {
77
        if ($definition->getPropertyType() !== Email::class) {
78
            $this->tagsPropertyService->fillPropertyDefinition($definition);
79
80
            return;
81
        }
82
83
        $eventConfiguration = $notification->getEventConfiguration();
84
85
        $identifier = $eventConfiguration['formDefinition'] ?? null;
86
87
        if (!$identifier) {
88
            return;
89
        }
90
91
        if (!$this->formPersistenceManager->exists($identifier)) {
92
            return;
93
        }
94
95
        $formDefinition = $this->getFormDefinition($identifier);
96
97
        /*
98
         * Unfortunately there is no getter method to fetch all elements from a
99
         * form definition, so we use this little hack to get them.
100
         */
101
        $elements = ObjectAccess::getProperty($formDefinition, 'elementsByIdentifier', true);
102
103
        foreach ($elements as $element) {
104
            /** @var FormElementInterface $element */
105
            $definition->addEntry($element->getIdentifier())
106
                ->setLabel($element->getLabel() . ' (' . $element->getIdentifier() . ')');
107
        }
108
    }
109
110
    /**
111
     * @param string $identifier
112
     * @return FormDefinition
113
     */
114
    protected function getFormDefinition(string $identifier): FormDefinition
115
    {
116
        $formDefinitionArray = $this->formPersistenceManager->load($identifier);
117
118
        return $this->arrayFormFactory->build($formDefinitionArray);
119
    }
120
}
121