Completed
Push — master ( f9056b...94589f )
by Julián
01:24
created

AggregateEventBehaviour::getCreatedAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Event\EventBehaviour;
17
use Gears\Event\Exception\EventException;
18
use Gears\EventSourcing\Aggregate\AggregateBehaviour;
19
use Gears\EventSourcing\Aggregate\AggregateVersion;
20
use Gears\Identity\Identity;
21
22
use function DeepCopy\deep_copy;
23
24
/**
25
 * Aggregate event behaviour.
26
 */
27
trait AggregateEventBehaviour
28
{
29
    use EventBehaviour, AggregateBehaviour {
30
        AggregateBehaviour::getIdentity as private;
31
        AggregateBehaviour::getVersion as private;
32
    }
33
34
    /**
35
     * Get aggregate id.
36
     *
37
     * @return Identity
38
     */
39
    final public function getAggregateId(): Identity
40
    {
41
        return $this->identity;
42
    }
43
44
    /**
45
     * Get aggregate version.
46
     *
47
     * @return AggregateVersion
48
     */
49
    final public function getAggregateVersion(): AggregateVersion
50
    {
51
        return $this->version;
52
    }
53
54
    /**
55
     * Get event with new aggregate version.
56
     *
57
     * @param AggregateVersion $aggregateVersion
58
     *
59
     * @throws \Gears\Event\Exception\EventException
60
     *
61
     * @return mixed|self
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use AggregateEventBehaviour.

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...
62
     */
63
    final public function withAggregateVersion(AggregateVersion $aggregateVersion)
64
    {
65
        if (!$this->version->isEqualTo(new AggregateVersion(0))) {
66
            throw new EventException(\sprintf(
67
                'Only new events can get a new version, event %s already at version %s',
68
                \get_class($this),
69
                $this->version->getValue()
70
            ));
71
        }
72
73
        if ($aggregateVersion->isEqualTo(new AggregateVersion(0))) {
0 ignored issues
show
Documentation introduced by
new \Gears\EventSourcing...ate\AggregateVersion(0) is of type object<Gears\EventSourci...egate\AggregateVersion>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
            throw new EventException(\sprintf(
75
                'Aggregate events can not get version 0 set, version 0 given to event %s',
76
                \get_class($this)
77
            ));
78
        }
79
80
        /* @var self $self */
81
        $self = deep_copy($this);
82
        $self->version = $aggregateVersion;
83
84
        return $self;
85
    }
86
}
87