Completed
Push — master ( 8a70c0...d1f51c )
by Rémi
02:48
created

DomainMessageListener::getContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace MessageApp\Listener;
4
5
use Broadway\Domain\DomainMessage;
6
use Broadway\EventHandling\EventListenerInterface;
7
use Broadway\Tools\Metadata\Context\ContextEnricher;
8
9
class DomainMessageListener implements EventListenerInterface
10
{
11
    /**
12
     * @var MessageEventHandler
13
     */
14
    private $handler;
15
16
    /**
17
     * Constructor
18
     *
19
     * @param MessageEventHandler $handler
20
     */
21
    public function __construct(MessageEventHandler $handler)
22
    {
23
        $this->handler = $handler;
24
    }
25
26
    /**
27
     * Handle an event.
28
     *
29
     * @param DomainMessage $message
30
     *
31
     * @return void
32
     */
33
    public function handle(DomainMessage $message)
34
    {
35
        $event = $message->getPayload();
36
        $context = static::getContext($message);
0 ignored issues
show
Bug introduced by
Since getContext() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getContext() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
37
38
        $this->handler->handle($event, $context);
39
    }
40
41
    /**
42
     * @param  DomainMessage $message
43
     *
44
     * @return mixed
45
     */
46
    private static function getContext(DomainMessage $message)
47
    {
48
        $metadataArray = $message->getMetadata()->serialize();
49
        $context = isset($metadataArray[ContextEnricher::CONTEXT]) ? $metadataArray[ContextEnricher::CONTEXT] : null;
50
51
        return $context;
52
    }
53
}
54