EventBehaviour   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
c 1
b 0
f 0
dl 0
loc 81
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMetadata() 0 3 1
A withAddedMetadata() 0 8 1
A getCreatedAt() 0 3 1
A addMetadata() 0 18 4
1
<?php
2
3
/*
4
 * event (https://github.com/phpgears/event).
5
 * Event handling.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/event
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\Event;
15
16
use Gears\DTO\Exception\InvalidScalarParameterException;
17
use Gears\DTO\ScalarPayloadBehaviour;
18
use Gears\Immutability\ImmutabilityBehaviour;
19
20
use function DeepCopy\deep_copy;
21
22
/**
23
 * Event metadata behaviour.
24
 */
25
trait EventBehaviour
26
{
27
    use ImmutabilityBehaviour, ScalarPayloadBehaviour {
28
        ScalarPayloadBehaviour::__call insteadof ImmutabilityBehaviour;
29
    }
30
31
    /**
32
     * Event metadata.
33
     *
34
     * @var array<string, mixed>
35
     */
36
    private $metadata = [];
37
38
    /**
39
     * @var \DateTimeImmutable
40
     */
41
    private $createdAt;
42
43
    /**
44
     * Get event metadata.
45
     *
46
     * @return array<string, mixed>
47
     */
48
    final public function getMetadata(): array
49
    {
50
        return $this->metadata;
51
    }
52
53
    /**
54
     * Get event with added metadata.
55
     *
56
     * @param array<string, mixed> $metadata
57
     *
58
     * @return mixed|self
59
     */
60
    final public function withAddedMetadata(array $metadata)
61
    {
62
        // @var self $self
63
        $self = deep_copy($this);
64
65
        $self->addMetadata($metadata);
66
67
        return $self;
68
    }
69
70
    /**
71
     * Set event metadata.
72
     *
73
     * @param array<string, mixed> $metadata
74
     *
75
     * @throws InvalidScalarParameterException
76
     */
77
    private function addMetadata(array $metadata): void
78
    {
79
        foreach ($metadata as $parameter => $value) {
80
            try {
81
                $this->checkParameterType($value);
82
            } catch (InvalidScalarParameterException $exception) {
83
                throw new InvalidScalarParameterException(
84
                    \sprintf(
85
                        'Class "%s" can only accept scalar metadata parameters, "%s" given.',
86
                        static::class,
87
                        \is_object($value) ? \get_class($value) : \gettype($value)
88
                    ),
89
                    $exception->getCode(),
90
                    $exception
91
                );
92
            }
93
94
            $this->metadata[$parameter] = $value;
95
        }
96
    }
97
98
    /**
99
     * Get event creation time.
100
     *
101
     * @return \DateTimeImmutable
102
     */
103
    final public function getCreatedAt(): \DateTimeImmutable
104
    {
105
        return $this->createdAt;
106
    }
107
}
108