Passed
Pull Request — master (#47)
by Romain
03:39
created

AbstractEvent::fillPropertyEntries()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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