AbstractEvent   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 26
dl 0
loc 128
rs 10
c 0
b 0
f 0

6 Methods

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