Completed
Push — master ( 054645...713312 )
by Ivannis Suárez
04:43
created

DomainEvent::eventName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cubiche\Domain\EventPublisher;
12
13
use Cubiche\Core\EventBus\Event\Event;
14
use DateTime;
15
16
/**
17
 * DomainEvent class.
18
 *
19
 * @author Ivannis Suárez Jerez <[email protected]>
20
 */
21
class DomainEvent extends Event implements DomainEventInterface
22
{
23
    /**
24
     * @var array
25
     */
26
    private $metadata = [];
27
28
    /**
29
     * @var array
30
     */
31
    private $payload = [];
32
33
    /**
34
     * DomainEvent constructor.
35
     */
36
    public function __construct()
37
    {
38
        $this->setMetadata('occurredOn', new DateTime());
39
        $this->setMetadata('eventType', parent::eventName());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (eventName() instead of __construct()). Are you sure this is correct? If so, you might want to change this to $this->eventName().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
40
        $this->setMetadata('propagationStopped', parent::isPropagationStopped());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (isPropagationStopped() instead of __construct()). Are you sure this is correct? If so, you might want to change this to $this->isPropagationStopped().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
41
    }
42
43
    /**
44
     * @return DateTime
45
     */
46
    public function occurredOn()
47
    {
48
        return $this->getMetadata('occurredOn');
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function eventName()
55
    {
56
        return $this->getMetadata('eventType');
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function stopPropagation()
63
    {
64
        $this->setMetadata('propagationStopped', true);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function isPropagationStopped()
71
    {
72
        return $this->getMetadata('propagationStopped');
73
    }
74
75
    /**
76
     * @param string $property
77
     * @param mixed  $value
78
     */
79
    protected function setMetadata($property, $value)
80
    {
81
        $this->metadata[$property] = $value;
82
    }
83
84
    /**
85
     * @param string $property
86
     *
87
     * @return mixed
88
     */
89
    protected function getMetadata($property)
90
    {
91
        return $this->metadata[$property];
92
    }
93
94
    /**
95
     * @param string $property
96
     * @param mixed  $value
97
     */
98
    protected function setPayload($property, $value)
99
    {
100
        $this->payload[$property] = $value;
101
    }
102
103
    /**
104
     * @param string $property
105
     *
106
     * @return mixed
107
     */
108
    protected function getPayload($property)
109
    {
110
        return $this->payload[$property];
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public static function fromArray(array $data)
117
    {
118
        $reflectionClass = new \ReflectionClass(static::class);
119
120
        /** @var DomainEvent $domainEvent */
121
        $domainEvent = $reflectionClass->newInstanceWithoutConstructor();
122
        $domainEvent->metadata = $data['metadata'];
123
        $domainEvent->payload = $data['payload'];
124
125
        return $domainEvent;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function toArray()
132
    {
133
        return array(
134
            'metadata' => $this->metadata,
135
            'payload' => $this->payload,
136
        );
137
    }
138
}
139