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

EnvelopeTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 65.38%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 56
ccs 17
cts 26
cp 0.6538
rs 10
c 1
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A withMessage() 0 6 1
A fromData() 0 5 1
A getData() 0 3 1
A getHandlerName() 0 3 1
A getMessage() 0 3 1
A getMetadata() 0 11 1
A getEnvelopeMetadata() 0 3 1
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