Passed
Pull Request — master (#214)
by Viktor
02:55
created

EnvelopeTrait::fromData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 5
ccs 0
cts 3
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
    /**
12
     * A mirror of {@see MessageInterface::fromData()}
13
     */
14
    abstract public static function fromMessage(MessageInterface $message): self;
15
16
    public static function fromData(string $handlerName, mixed $data, array $metadata = []): MessageInterface
17
    {
18
        $message = new Message($handlerName, $data, $metadata);
19
20
        return self::fromMessage($message);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::fromMessage($message) returns the type Yiisoft\Queue\Message\EnvelopeTrait which is incompatible with the type-hinted return Yiisoft\Queue\Message\MessageInterface.
Loading history...
21
    }
22
23 5
    public function getMessage(): MessageInterface
24
    {
25 5
        return $this->message;
26
    }
27
28
    public function withMessage(MessageInterface $message): self
29
    {
30
        $instance = clone $this;
31
        $instance->message = $message;
32
33
        return $instance;
34
    }
35
36 9
    public function getHandlerName(): string
37
    {
38 9
        return $this->message->getHandlerName();
39
    }
40
41 5
    public function getData(): mixed
42
    {
43 5
        return $this->message->getData();
44
    }
45
46 13
    public function getMetadata(): array
47
    {
48 13
        return array_merge(
49 13
            $this->message->getMetadata(),
50 13
            [
51 13
                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...
52 13
                    $this->message->getMetadata()[self::ENVELOPE_STACK_KEY] ?? [],
53 13
                    [self::class],
54 13
                ),
55 13
            ],
56 13
            $this->getEnvelopeMetadata(),
57 13
        );
58
    }
59
60
    public function getEnvelopeMetadata(): array
61
    {
62
        return [];
63
    }
64
}
65