Passed
Pull Request — master (#97)
by Romain
03:28
created

DispatchFormNotificationEvent::run()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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