Passed
Pull Request — master (#62)
by Romain
05:52 queued 02:14
created

DispatchFormNotificationEvent   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

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