Passed
Push — master ( 0b20e6...3a20e7 )
by Aimeos
05:22 queued 55s
created

Publisher::attach()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019-2023
6
 * @package MShop
7
 * @subpackage Order
8
 */
9
10
11
namespace Aimeos\MShop\Order\Item;
12
13
14
/**
15
 * Default implementation of a publisher in the observer pattern
16
 *
17
 * @package MShop
18
 * @subpackage Observer
19
 */
20
trait Publisher
21
{
22
	protected array $listeners = [];
23
24
25
	/**
26
	 * Adds a listener object to the publisher.
27
	 *
28
	 * @param \Aimeos\MShop\Plugin\Provider\Iface $l Object implementing listener interface
29
	 * @param string $action Name of the action to listen for
30
	 * @return \Aimeos\MShop\Order\Item\Iface Publisher object for method chaining
31
	 */
32
	public function attach( \Aimeos\MShop\Plugin\Provider\Iface $l, string $action ) : Iface
33
	{
34
		$this->listeners[$action][] = $l;
35
		return $this;
36
	}
37
38
39
	/**
40
	 * Removes all attached listeners from the publisher
41
	 *
42
	 * @return \Aimeos\MShop\Order\Item\Iface Publisher object for method chaining
43
	 */
44
	public function off() : Iface
45
	{
46
		$this->listeners = [];
47
		return $this;
48
	}
49
50
51
	/**
52
	 * Sends updates to all listeners of the given action.
53
	 *
54
	 * @param string $action Name of the action
55
	 * @param mixed $value Value or object given to the listeners
56
	 * @return mixed Modified value parameter
57
	 */
58
	protected function notify( string $action, $value = null )
59
	{
60
		if( isset( $this->listeners[$action] ) )
61
		{
62
			foreach( $this->listeners[$action] as $key => $listener ) {
63
				$value = $listener->update( $this, $action, $value );
64
			}
65
		}
66
67
		return $value;
68
	}
69
}
70