EntityEvent::setEntityClassName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Polder Knowledge / entityservice (https://polderknowledge.com)
4
 *
5
 * @link https://github.com/polderknowledge/entityservice for the canonical source repository
6
 * @copyright Copyright (c) 2016 Polder Knowledge (https://polderknowledge.com)
7
 * @license https://github.com/polderknowledge/entityservice/blob/master/LICENSE.md MIT
8
 */
9
10
namespace PolderKnowledge\EntityService\Event;
11
12
use ArrayAccess;
13
use PolderKnowledge\EntityService\Exception\RuntimeException;
14
use Zend\EventManager\Event;
15
16
/**
17
 * The representation of an event with an entity.
18
 */
19
final class EntityEvent extends Event
20
{
21
    /**
22
     * The FQCN of the entity that this event handles.
23
     *
24
     * @var string
25
     */
26
    private $entityClassName;
27
28
    /**
29
     * A flag that indicates whether or not propagation can be stopped.
30
     *
31
     * @var bool
32
     */
33
    private $canStopPropagation;
34
35
    /**
36
     * The error message that can occur when working with the entity.
37
     *
38
     * @var string
39
     */
40
    private $error;
41
42
    /**
43
     * The error code of the error that can occur when working with the entity.
44
     *
45
     * @var integer
46
     */
47
    private $errornr;
48
49
    /**
50
     * The result of a service manager call.
51
     *
52
     * @var mixed
53
     */
54
    private $result;
55
56
    /**
57
     * Initializes a new instance of this class.
58
     * Accepts a target and its parameters.
59
     *
60
     * @param string $name Event name The name of the event.
61
     * @param string|object $target The target of the event.
62
     * @param array|ArrayAccess $params Additional parameters of this event.
63
     */
64 159
    public function __construct($name = null, $target = null, $params = null)
65
    {
66 159
        parent::__construct($name, $target, $params);
67
68 159
        $this->canStopPropagation = true;
69 159
    }
70
71
    /**
72
     * Gets the list with entities that are the result of this event.
73
     *
74
     * @return mixed Returns the result of a service manager call.
75
     */
76 54
    public function getResult()
77
    {
78 54
        return $this->result;
79
    }
80
81
    /**
82
     * Sets the list with entities that are the result of this event.
83
     *
84
     * @param mixed $result The result of a service manager call.
85
     * @return EntityEvent Returns the instance of this class so that chaining can be used.
86
     */
87 42
    public function setResult($result)
88
    {
89 42
        $this->result = $result;
90 42
        return $this;
91
    }
92
93
    /**
94
     * Gets the FQCN of the entity that is used.
95
     *
96
     * @return string Returns a string containing the FQCN.
97
     */
98 105
    public function getEntityClassName()
99
    {
100 105
        return $this->entityClassName;
101
    }
102
103
    /**
104
     * Sets the FQCN of the entity that is used.
105
     *
106
     * @param string $name The FQCN of the entity.
107
     * @return EntityEvent Returns the instance of this class so that chaining can be used.
108
     * @throws RuntimeException
109
     */
110 117
    public function setEntityClassName($name)
111
    {
112 117
        $trimmedName = trim($name, '\\');
113
114 117
        if (!class_exists($trimmedName)) {
115 3
            throw new RuntimeException(sprintf('The class "%s" does not exist.', $trimmedName));
116
        }
117
118 114
        $this->entityClassName = $trimmedName;
119
120 114
        return $this;
121
    }
122
123
    /**
124
     * Enables or disables further event propagation.
125
     *
126
     * @param bool $flag A flag that indicates whether or not to stop propagation.
127
     * @return void
128
     */
129 54
    public function stopPropagation($flag = true)
130
    {
131 54
        if ($this->canStopPropagation) {
132 51
            parent::stopPropagation($flag);
133
        }
134 54
    }
135
136
    /**
137
     * Disable the option of stopping the event propagation
138
     *
139
     * @return self
140
     */
141 48
    public function disableStoppingOfPropagation()
142
    {
143 48
        $this->canStopPropagation = false;
144 48
        return $this;
145
    }
146
147
    /**
148
     * Gets the message of the error that occurred when working with the entity.
149
     *
150
     * @return string
151
     */
152 6
    public function getError()
153
    {
154 6
        return $this->error;
155
    }
156
157
    /**
158
     * Sets the message of the error that occurred when working with the entity.
159
     *
160
     * @param string $error The error message to set.
161
     * @return self
162
     */
163 9
    public function setError($error)
164
    {
165 9
        $this->error = (string)$error;
166 9
        return $this;
167
    }
168
169
    /**
170
     * Gets the error number of the error that occurred when working with the entity.
171
     *
172
     * @return integer
173
     */
174 9
    public function getErrorNr()
175
    {
176 9
        return $this->errornr;
177
    }
178
179
    /**
180
     * Sets the error number of the error that occurred when working with the entity.
181
     *
182
     * @param integer $errorNr The error number to set.
183
     * @return EntityEvent Returns the instance of this class so that chaining can be used.
184
     */
185 12
    public function setErrorNr($errorNr)
186
    {
187 12
        $this->errornr = (int)$errorNr;
188 12
        return $this;
189
    }
190
191
    /**
192
     * Checks if error information is set.
193
     *
194
     * @return bool Returns true when error information is set; false otherwise.
195
     */
196 12
    public function isError()
197
    {
198 12
        return (null !== $this->errornr || null !== $this->error);
199
    }
200
}
201