Passed
Push — master ( a2046d...d2f9de )
by Blizzz
13:01 queued 12s
created

GenericEvent::setArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2019 Arthur Schiwon <[email protected]>
5
 *
6
 * @author Arthur Schiwon <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCP\EventDispatcher;
26
27
use ArrayAccess;
28
use ArrayIterator;
29
use InvalidArgumentException;
30
use IteratorAggregate;
31
use Traversable;
32
use function array_key_exists;
33
34
/**
35
 * Class GenericEvent
36
 *
37
 * convenience reimplementation of \Symfony\Component\GenericEvent against
38
 * \OCP\EventDispatcher\Event
39
 *
40
 * @package OCP\EventDispatcher
41
 * @since 18.0.0
42
 */
43
class GenericEvent extends Event implements ArrayAccess, IteratorAggregate {
44
	protected $subject;
45
	protected $arguments;
46
47
	/**
48
	 * Encapsulate an event with $subject and $args.
49
	 *
50
	 * @since 18.0.0
51
	 */
52
	public function __construct($subject = null, array $arguments = []) {
53
		$this->subject = $subject;
54
		$this->arguments = $arguments;
55
	}
56
57
	/**
58
	 * Getter for subject property.
59
	 *
60
	 * @since 18.0.0
61
	 */
62
	public function getSubject() {
63
		return $this->subject;
64
	}
65
66
	/**
67
	 * Get argument by key.
68
	 *
69
	 * @throws InvalidArgumentException if key is not found
70
	 * @since 18.0.0
71
	 */
72
	public function getArgument(string $key) {
73
		if ($this->hasArgument($key)) {
74
			return $this->arguments[$key];
75
		}
76
77
		throw new InvalidArgumentException(sprintf('Argument "%s" not found.', $key));
78
	}
79
80
	/**
81
	 * Add argument to event.
82
	 *
83
	 * @since 18.0.0
84
	 */
85
	public function setArgument($key, $value): GenericEvent {
86
		$this->arguments[$key] = $value;
87
		return $this;
88
	}
89
90
	/**
91
	 * Getter for all arguments.
92
	 *
93
	 * @since 18.0.0
94
	 */
95
	public function getArguments(): array {
96
		return $this->arguments;
97
	}
98
99
	/**
100
	 * Set args property.
101
	 *
102
	 * @since 18.0.0
103
	 */
104
	public function setArguments(array $args = []): GenericEvent {
105
		$this->arguments = $args;
106
		return $this;
107
	}
108
109
	/**
110
	 * Has argument.
111
	 *
112
	 * @since 18.0.0
113
	 */
114
	public function hasArgument($key): bool {
115
		return array_key_exists($key, $this->arguments);
116
	}
117
118
	/**
119
	 * Retrieve an external iterator
120
	 *
121
	 * @link https://php.net/manual/en/iteratoraggregate.getiterator.php
122
	 * @since 18.0.0
123
	 */
124
	public function getIterator(): Traversable {
125
		return new ArrayIterator($this->arguments);
126
	}
127
128
	/**
129
	 * Whether a offset exists
130
	 *
131
	 * @link https://php.net/manual/en/arrayaccess.offsetexists.php
132
	 * @since 18.0.0
133
	 */
134
	public function offsetExists($offset): bool {
135
		return $this->hasArgument($offset);
136
	}
137
138
	/**
139
	 * Offset to retrieve
140
	 *
141
	 * @link https://php.net/manual/en/arrayaccess.offsetget.php
142
	 * @since 18.0.0
143
	 */
144
	public function offsetGet($offset) {
145
		return $this->arguments[$offset];
146
	}
147
148
	/**
149
	 * Offset to set
150
	 *
151
	 * @link https://php.net/manual/en/arrayaccess.offsetset.php
152
	 * @since 18.0.0
153
	 */
154
	public function offsetSet($offset, $value): void {
155
		$this->setArgument($offset, $value);
156
	}
157
158
	/**
159
	 * Offset to unset
160
	 *
161
	 * @link https://php.net/manual/en/arrayaccess.offsetunset.php
162
	 * @since 18.0.0
163
	 */
164
	public function offsetUnset($offset): void {
165
		if ($this->hasArgument($offset)) {
166
			unset($this->arguments[$offset]);
167
		}
168
	}
169
}
170