Completed
Push — master ( 0fe229...42b9af )
by Julián
11:50
created

EventBehaviour   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 84
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMetadata() 0 4 1
A withMetadata() 0 8 1
A setMetadata() 0 22 4
A getCreatedAt() 0 4 1
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>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
47
     */
48
    final public function getMetadata(): array
49
    {
50
        return $this->metadata;
51
    }
52
53
    /**
54
     * Get event with new metadata.
55
     *
56
     * @param array<string, mixed> $metadata
57
     *
58
     * @return mixed|self
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use EventBehaviour.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
59
     */
60
    final public function withMetadata(array $metadata)
61
    {
62
        /* @var self $self */
63
        $self = deep_copy($this);
64
        $self->setMetadata($metadata);
65
66
        return $self;
67
    }
68
69
    /**
70
     * Set event metadata.
71
     *
72
     * @param array<string, mixed> $parameters
73
     *
74
     * @throws InvalidScalarParameterException
75
     */
76
    private function setMetadata(array $parameters): void
77
    {
78
        $this->metadata = [];
79
80
        foreach ($parameters as $parameter => $value) {
81
            try {
82
                $this->checkParameterType($value);
83
            } catch (InvalidScalarParameterException $exception) {
84
                throw new InvalidScalarParameterException(
85
                    \sprintf(
86
                        'Class %s can only accept scalar metadata parameters, %s given',
87
                        self::class,
88
                        \is_object($value) ? \get_class($value) : \gettype($value)
89
                    ),
90
                    $exception->getCode(),
91
                    $exception
92
                );
93
            }
94
95
            $this->metadata[$parameter] = $value;
96
        }
97
    }
98
99
    /**
100
     * Get event creation time.
101
     *
102
     * @return \DateTimeImmutable
103
     */
104
    final public function getCreatedAt(): \DateTimeImmutable
105
    {
106
        return $this->createdAt;
107
    }
108
}
109