Passed
Pull Request — master (#59)
by Romain
03:35
created

AbstractEvent::getNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\Core\Event;
18
19
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition;
20
use CuyZ\Notiz\Core\Event\Exception\CancelEventDispatch;
21
use CuyZ\Notiz\Core\Notification\Notification;
22
use CuyZ\Notiz\Core\Property\Factory\PropertyContainer;
23
use CuyZ\Notiz\Core\Property\Factory\PropertyDefinition;
24
use CuyZ\Notiz\Core\Property\Factory\PropertyFactory;
25
use CuyZ\Notiz\Core\Property\PropertyEntry;
26
use CuyZ\Notiz\Core\Property\Service\TagsPropertyService;
27
use TYPO3\CMS\Extbase\Object\ObjectManager;
28
29
/**
30
 * Default event implementation provided by this extension, you can use it for
31
 * your own events.
32
 *
33
 * It does implement all methods needed by the interface, so you wont have to do
34
 * the work yourself, unless you want to override something.
35
 *
36
 * You may still implement your own method named `run()` where you can process
37
 * the logic of the event (usually used to fill the properties of the class).
38
 *
39
 * Tag property service
40
 * --------------------
41
 *
42
 * By default, the property definitions are handled by the tag property service,
43
 * that will analyze your event class attributes and their annotations to
44
 * automatically fill the definitions.
45
 *
46
 * @see \CuyZ\Notiz\Core\Property\Service\TagsPropertyService
47
 *
48
 * The only thing you need to do is add the correct annotations on the class
49
 * attributes you want to use, and fill them with the correct values during the
50
 * dispatch process.
51
 *
52
 * Example:
53
 *
54
 * ```
55
 * /**
56
 *  * @var string
57
 *  *
58
 *  * @marker
59
 *  * /
60
 * protected $userName;
61
 *
62
 * public function run(UserObject $userObject)
63
 * {
64
 *     $this->userName = $userObject->getName();
65
 * }
66
 * ```
67
 *
68
 * Cancel dispatch
69
 * ---------------
70
 *
71
 * In the implementation of the method `run()` you can call the method
72
 * `cancelDispatch()` whenever you need to. This will cancel the dispatch of the
73
 * event and prevent any notification bound to this event from being fired.
74
 */
75
abstract class AbstractEvent implements Event
76
{
77
    /**
78
     * @var EventDefinition
79
     */
80
    protected $eventDefinition;
81
82
    /**
83
     * @var Notification
84
     */
85
    protected $notification;
86
87
    /**
88
     * @var array
89
     */
90
    protected $configuration = [];
91
92
    /**
93
     * @var PropertyFactory
94
     */
95
    protected $propertyFactory;
96
97
    /**
98
     * @var ObjectManager
99
     */
100
    protected $objectManager;
101
102
    /**
103
     * WARNING
104
     * -------
105
     *
106
     * If you need to override the constructor, do not forget to call:
107
     * `parent::__construct`
108
     *
109
     * @param EventDefinition $eventDefinition
110
     * @param Notification $notification
111
     * @param PropertyFactory $propertyFactory
112
     * @param ObjectManager $objectManager
113
     */
114
    public function __construct(EventDefinition $eventDefinition, Notification $notification, PropertyFactory $propertyFactory, ObjectManager $objectManager)
115
    {
116
        $this->eventDefinition = $eventDefinition;
117
        $this->notification = $notification;
118
        $this->configuration = $notification->getEventConfiguration();
119
        $this->propertyFactory = $propertyFactory;
120
        $this->objectManager = $objectManager;
121
    }
122
123
    /**
124
     * This method can be called from the method `run()` to cancel the dispatch
125
     * of the event.
126
     *
127
     * You may call this under certain conditions of your own if they need the
128
     * event not to be dispatched, preventing the notifications bound to this
129
     * event to be fired.
130
     *
131
     * @throws CancelEventDispatch
132
     */
133
    protected function cancelDispatch()
134
    {
135
        throw new CancelEventDispatch;
136
    }
137
138
    /**
139
     * See class description for more information.
140
     *
141
     * @param PropertyDefinition $definition
142
     */
143
    public static function buildPropertyDefinition(PropertyDefinition $definition)
144
    {
145
        TagsPropertyService::get()->fillPropertyDefinition($definition);
146
    }
147
148
    /**
149
     * Fills the property container with the values from the class attributes.
150
     *
151
     * @param PropertyContainer $container
152
     */
153
    public function fillPropertyEntries(PropertyContainer $container)
154
    {
155
        foreach ($container->getEntries() as $property) {
156
            $name = $property->getName();
157
158
            if (property_exists($this, $name)) {
159
                $property->setValue($this->$name);
160
            }
161
        }
162
    }
163
164
    /**
165
     * @param string $propertyClassName
166
     * @return PropertyEntry[]
167
     */
168
    public function getProperties($propertyClassName)
169
    {
170
        return $this->propertyFactory->getProperties($propertyClassName, $this);
171
    }
172
173
    /**
174
     * @return EventDefinition
175
     */
176
    public function getDefinition()
177
    {
178
        return $this->eventDefinition;
179
    }
180
181
    /**
182
     * @return Notification
183
     */
184
    public function getNotification()
185
    {
186
        return $this->notification;
187
    }
188
}
189