DomainEventCollection   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 59
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAll() 0 4 1
A getDomainEventOfType() 0 15 3
A getDomainEventsOfType() 0 11 3
A count() 0 4 1
1
<?php declare(strict_types = 1);
2
3
namespace TomCizek\ResponseRecorder\Application\ResponseRecorder\MutationResponse;
4
5
use Countable;
6
use Prooph\Common\Messaging\DomainEvent;
7
use TomCizek\ResponseRecorder\Application\ResponseRecorder\MutationResponse\DomainEventCollection\MoreThanOneEventOfRequestedTypeExists;
8
9
class DomainEventCollection implements Countable
10
{
11
	/** @var DomainEvent[] */
12
	protected $domainEvents = [];
13
14
	/**
15
	 * @param DomainEvent[] $domainEvents
16
	 */
17 14
	public function __construct(array $domainEvents)
18
	{
19 14
		$this->domainEvents = $domainEvents;
20 14
	}
21
22
	/**
23
	 * @return DomainEvent[]
24
	 */
25 1
	public function getAll(): array
26
	{
27 1
		return $this->domainEvents;
28
	}
29
30 3
	public function getDomainEventOfType(string $type): ?DomainEvent
31
	{
32 3
		$ofType = $this->getDomainEventsOfType($type);
33
34 3
		if (count($ofType) > 1) {
35 1
			throw new MoreThanOneEventOfRequestedTypeExists(
36 1
				sprintf('More than one event of requested type %s exists.', $type)
37
			);
38
		}
39 2
		if (isset($ofType[0])) {
40 1
			return $ofType[0];
41
		}
42
43 1
		return null;
44
	}
45
46
	/**
47
	 * @param string $type
48
	 *
49
	 * @return DomainEvent[]
50
	 */
51 5
	public function getDomainEventsOfType(string $type): array
52
	{
53 5
		$ofType = [];
54 5
		foreach ($this->domainEvents as $event) {
55 3
			if (is_a($event, $type)) {
56 3
				$ofType[] = $event;
57
			}
58
		}
59
60 5
		return $ofType;
61
	}
62
63 9
	public function count(): int
64
	{
65 9
		return count($this->domainEvents);
66
	}
67
}
68
69
70