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

Publisher   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 1
b 0
f 0
dl 0
loc 48
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A notify() 0 10 3
A off() 0 4 1
A attach() 0 4 1
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