Completed
Push — master ( d92413...ff1eea )
by Beñat
01:42
created

StoredEvent   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 58
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromDomainEvent() 0 7 1
A setPayload() 0 17 3
A __construct() 0 7 1
A setOccurredOn() 0 5 1
A toArray() 0 9 1
1
<?php
2
3
/*
4
 * This file is part of the Shared Kernel library.
5
 *
6
 * Copyright (c) 2016-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace LIN3S\SharedKernel\Event;
15
16
use LIN3S\SharedKernel\Domain\Model\DomainEvent;
17
18
/**
19
 * @author Beñat Espiña <[email protected]>
20
 */
21
class StoredEvent
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
22
{
23
    private $id;
24
    private $type;
25
    private $payload;
26
    private $occurredOn;
27
    private $stream;
28
29
    public static function fromDomainEvent(DomainEvent $event, StreamName $stream) : self
30
    {
31
        $instance = new self(get_class($event), $event->occurredOn(), $stream);
32
        $instance->setPayload($event);
33
34
        return $instance;
35
    }
36
37
    private function setPayload(DomainEvent $event) : void
38
    {
39
        $eventReflection = new \ReflectionClass($event);
40
        foreach ($eventReflection->getProperties() as $property) {
41
            if ('occurredOn' === $property->name) {
42
                continue;
43
            }
44
            $property->setAccessible(true);
45
            // TODO: the following cast is not valid
46
            // what happens when the given property (Value object)
47
            // contains more than one attribute or it has not got implemented
48
            // the __toString() method ??
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49
            // Furthermore, in the decodification process we need the class this property
50
            $this->payload[$property->getName()] = (string) $property->getValue($event);
51
        }
52
        $this->payload = json_encode($this->payload);
53
    }
54
55
    private function __construct(string $type, \DateTimeInterface $occurredOn, StreamName $stream)
56
    {
57
        $this->type = $type;
58
        $this->payload = [];
59
        $this->setOccurredOn($occurredOn);
60
        $this->stream = $stream->name();
61
    }
62
63
    private function setOccurredOn(\DateTimeInterface $occurredOn) : void
64
    {
65
        $occurredOn->setTimezone(new \DateTimeZone('UTC'));
66
        $this->occurredOn = $occurredOn->getTimestamp();
67
    }
68
69
    public function toArray() : array
70
    {
71
        return [
72
            $this->type,
73
            $this->payload,
74
            $this->occurredOn,
75
            $this->stream,
76
        ];
77
    }
78
}
79