DispatchFormNotificationEvent   A
last analyzed

Complexity

Total Complexity 9

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 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getExampleProperties() 0 6 1
A run() 0 13 3
A injectFormPersistenceManager() 0 3 1
A fillPropertyEntries() 0 11 4
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\Event\AbstractEvent;
21
use CuyZ\Notiz\Core\Event\Support\ProvidesExampleProperties;
22
use CuyZ\Notiz\Core\Property\Factory\PropertyContainer;
23
use CuyZ\Notiz\Domain\Property\Email;
24
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext 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...
25
use TYPO3\CMS\Form\Domain\Finishers\FinisherContext;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Form\Domain\Finishers\FinisherContext 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...
26
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Form\Domain\Runtime\FormRuntime 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\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...
28
29
/**
30
 * This event is triggered when a form that implements the notification dispatch
31
 * finisher is submitted.
32
 *
33
 * The identifier configured in the finisher must be the same as the identifier
34
 * in this event options, or the event is canceled.
35
 */
36
class DispatchFormNotificationEvent extends AbstractEvent implements ProvidesExampleProperties
37
{
38
    /**
39
     * @label Event/Form:dispatch_form_notification.marker.form_values
40
     * @marker
41
     *
42
     * @var array
43
     */
44
    protected $formValues;
45
46
    /**
47
     * @label Event/Form:dispatch_form_notification.marker.form_runtime
48
     * @marker
49
     *
50
     * @var FormRuntime
51
     */
52
    protected $formRuntime;
53
54
    /**
55
     * @label Event/Form:dispatch_form_notification.marker.controller_context
56
     * @marker
57
     *
58
     * @var ControllerContext
59
     */
60
    protected $controllerContext;
61
62
    /**
63
     * @var FormPersistenceManagerInterface
64
     */
65
    protected $formPersistenceManager;
66
67
    /**
68
     * @param FinisherContext $finisherContext
69
     */
70
    public function run(FinisherContext $finisherContext)
71
    {
72
        $identifier = $this->configuration['formDefinition'];
73
        $this->formRuntime = $finisherContext->getFormRuntime();
74
75
        if (!$identifier
76
            || $this->formRuntime->getFormDefinition()->getPersistenceIdentifier() !== $identifier
77
        ) {
78
            $this->cancelDispatch();
79
        }
80
81
        $this->formValues = $finisherContext->getFormValues();
82
        $this->controllerContext = $finisherContext->getControllerContext();
83
    }
84
85
    /**
86
     * Adds the fields values to the email properties so they can be used as
87
     * recipients for email notifications.
88
     *
89
     * @param PropertyContainer $container
90
     */
91
    public function fillPropertyEntries(PropertyContainer $container)
92
    {
93
        parent::fillPropertyEntries($container);
94
95
        if ($container->getPropertyType() !== Email::class) {
96
            return;
97
        }
98
99
        foreach ($this->formValues as $key => $value) {
100
            if ($container->hasEntry($key)) {
101
                $container->getEntry($key)->setValue($value);
102
            }
103
        }
104
    }
105
106
    /**
107
     * @param FormPersistenceManagerInterface $formPersistenceManager
108
     */
109
    public function injectFormPersistenceManager(FormPersistenceManagerInterface $formPersistenceManager)
110
    {
111
        $this->formPersistenceManager = $formPersistenceManager;
112
    }
113
114
    /**
115
     * @return array
116
     */
117
    public function getExampleProperties(): array
118
    {
119
        return [
120
            'formValues' => [
121
                'name' => 'John Doe',
122
                'email' => '[email protected]',
123
            ],
124
        ];
125
    }
126
}
127