Passed
Pull Request — master (#216)
by Viktor
03:05
created

EnvelopeTrait::getEnvelopeMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Queue\Message;
6
7
trait EnvelopeTrait
8
{
9
    private MessageInterface $message;
10
11 4
    public function getMessage(): MessageInterface
12
    {
13 4
        return $this->message;
14
    }
15
16
    public function withMessage(MessageInterface $message): self
17
    {
18
        $instance = clone $this;
19
        $instance->message = $message;
20
21
        return $instance;
22
    }
23
24 8
    public function getHandlerName(): string
25
    {
26 8
        return $this->message->getHandlerName();
27
    }
28
29 4
    public function getData(): mixed
30
    {
31 4
        return $this->message->getData();
32
    }
33
34 12
    public function getMetadata(): array
35
    {
36 12
        return array_merge(
37 12
            $this->message->getMetadata(),
38 12
            [
39 12
                self::ENVELOPE_STACK_KEY => array_merge(
0 ignored issues
show
Bug introduced by
The constant Yiisoft\Queue\Message\En...ait::ENVELOPE_STACK_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
40 12
                    $this->message->getMetadata()[self::ENVELOPE_STACK_KEY] ?? [],
41 12
                    [self::class],
42 12
                ),
43 12
            ],
44 12
            $this->getEnvelopeMetadata(),
45 12
        );
46
    }
47
48
    /**
49
     * Finds an envelope in the current envelope stack or creates a new one from the message.
50
     *
51
     * @template T
52
     *
53
     * @psalm-param T<class-string<EnvelopeInterface>> $className
54
     * @throws NotEnvelopInterfaceException is thrown if the given class does not implement {@see EnvelopeInterface}.
55
     *
56
     * @psalm-return T
57
     */
58 4
    public function getEnvelopeFromStack(string $className): EnvelopeInterface
59
    {
60 4
        if (!is_a($className, EnvelopeInterface::class, true)) {
61 1
            throw new NotEnvelopInterfaceException($className);
62
        }
63
64 3
        if (static::class === $className) {
65 2
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Yiisoft\Queue\Message\EnvelopeTrait which is incompatible with the type-hinted return Yiisoft\Queue\Message\EnvelopeInterface.
Loading history...
66
        }
67
68 3
        if ($this->message instanceof EnvelopeInterface) {
69 2
            return $this->message->getEnvelopeFromStack($className);
0 ignored issues
show
Bug introduced by
The method getEnvelopeFromStack() does not exist on Yiisoft\Queue\Message\MessageInterface. It seems like you code against a sub-type of Yiisoft\Queue\Message\MessageInterface such as Yiisoft\Queue\Message\EnvelopeInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            return $this->message->/** @scrutinizer ignore-call */ getEnvelopeFromStack($className);
Loading history...
70
        }
71
72 1
        return $className::fromMessage($this->message);
73
    }
74
75 2
    public static function getEnvelopeFromMessage(MessageInterface $message): EnvelopeInterface
76
    {
77 2
        if ($message instanceof EnvelopeInterface) {
78 1
            return $message->getEnvelopeFromStack(static::class);
79
        }
80
81 1
        return static::fromMessage($message);
82
    }
83
84
    public function getEnvelopeMetadata(): array
85
    {
86
        return [];
87
    }
88
}
89