Completed
Push — master ( 7b9431...f9056b )
by Julián
01:23
created

AbstractAggregateEvent::withAggregateVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * event-sourcing (https://github.com/phpgears/event-sourcing).
5
 * Event Sourcing base.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/event-sourcing
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\EventSourcing\Event;
15
16
use Gears\DTO\ScalarPayloadBehaviour;
17
use Gears\Event\Time\SystemTimeProvider;
18
use Gears\Event\Time\TimeProvider;
19
use Gears\EventSourcing\Aggregate\AggregateVersion;
20
use Gears\Identity\Identity;
21
use Gears\Immutability\ImmutabilityBehaviour;
22
23
/**
24
 * Abstract immutable aggregate event.
25
 */
26
abstract class AbstractAggregateEvent implements AggregateEvent
27
{
28
    use ImmutabilityBehaviour, ScalarPayloadBehaviour, AggregateEventBehaviour {
29
        ScalarPayloadBehaviour::__call insteadof ImmutabilityBehaviour;
30
    }
31
32
    /**
33
     * Prevent aggregate event direct instantiation.
34
     *
35
     * @param Identity             $aggregateId
36
     * @param AggregateVersion     $aggregateVersion
37
     * @param array<string, mixed> $payload
38
     * @param \DateTimeImmutable   $createdAt
39
     */
40
    final protected function __construct(
41
        Identity $aggregateId,
42
        AggregateVersion $aggregateVersion,
43
        array $payload,
44
        \DateTimeImmutable $createdAt
45
    ) {
46
        $this->checkImmutability();
47
48
        $this->identity = $aggregateId;
49
        $this->version = $aggregateVersion;
50
        $this->createdAt = $createdAt->setTimezone(new \DateTimeZone('UTC'));
0 ignored issues
show
Documentation Bug introduced by
It seems like $createdAt->setTimezone(new \DateTimeZone('UTC')) can also be of type false. However, the property $createdAt is declared as type object<DateTimeImmutable>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
51
52
        $this->setPayload($payload);
53
    }
54
55
    /**
56
     * Instantiate new aggregate event.
57
     *
58
     * @param Identity             $aggregateId
59
     * @param array<string, mixed> $payload
60
     * @param TimeProvider|null    $timeProvider
61
     *
62
     * @return mixed|self
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use AbstractAggregateEvent.

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...
63
     */
64
    final protected static function occurred(Identity $aggregateId, array $payload, ?TimeProvider $timeProvider = null)
65
    {
66
        $timeProvider = $timeProvider ?? new SystemTimeProvider();
67
68
        return new static(
69
            $aggregateId,
70
            new AggregateVersion(0),
71
            $payload,
72
            $timeProvider->getCurrentTime()
73
        );
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     *
79
     * @return mixed|self
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use AbstractAggregateEvent.

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...
80
     */
81
    final public static function reconstitute(array $payload, array $attributes = [])
82
    {
83
        return new static(
84
            $attributes['aggregateId'],
85
            $attributes['aggregateVersion'],
86
            $payload,
87
            $attributes['createdAt']
88
        );
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     *
94
     * @return string[]
95
     */
96
    final protected function getAllowedInterfaces(): array
97
    {
98
        return [AggregateEvent::class];
99
    }
100
}
101