Completed
Pull Request — master (#143)
by Kristof
09:50 queued 04:32
created

PayloadIsInstanceOf   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 42
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A isSatisfiedBy() 0 17 3
1
<?php
2
/**
3
 * @file
4
 */
5
6
namespace CultuurNet\UDB3\EventHandling\DomainMessage;
7
8
use Broadway\Domain\DomainMessage;
9
use Psr\Log\LoggerAwareInterface;
10
use Psr\Log\LoggerAwareTrait;
11
use Psr\Log\NullLogger;
12
13
class PayloadIsInstanceOf implements SpecificationInterface, LoggerAwareInterface
14
{
15
    use LoggerAwareTrait;
16
17
    /**
18
     * @var string
19
     */
20
    private $typeName;
21
22
    /**
23
     * @param string $typeName
24
     */
25
    public function __construct($typeName)
26
    {
27
        if (!is_string($typeName)) {
28
            throw new \InvalidArgumentException('Value for argument typeName should be a string');
29
        }
30
        $this->typeName = $typeName;
31
        $this->logger = new NullLogger();
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function isSatisfiedBy(DomainMessage $domainMessage)
38
    {
39
        $payload = $domainMessage->getPayload();
40
41
        $payloadClass = get_class($payload);
42
        $this->logger->info(
43
            "expected: {$this->typeName}, actual: {$payloadClass}"
44
        );
45
46
        $satisfied =
47
            is_a($payload, $this->typeName) ||
48
            is_subclass_of($payload, $this->typeName);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $this->typeName can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
49
50
        $this->logger->info('satisfied: ' . ($satisfied ? 'yes' : 'no'));
51
52
        return $satisfied;
53
    }
54
}
55