Passed
Pull Request — master (#217)
by Viktor
03:30
created

IdEnvelope::setId()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Queue\Message;
6
7
/**
8
 * ID envelope allows to identify a message.
9
 */
10
final class IdEnvelope extends AbstractEnvelope
11
{
12
    public const MESSAGE_ID_KEY = 'yii-message-id';
13
14 13
    public function __construct(
15
        MessageInterface $message,
16
        private readonly string|int|null $id = null,
17
    ) {
18 13
        parent::__construct($message);
19
    }
20
21 2
    public static function fromMessage(MessageInterface $message): self
22
    {
23 2
        return new self($message, $message->getMetadata()[self::MESSAGE_ID_KEY] ?? null);
24
    }
25
26 12
    public function getId(): string|int|null
27
    {
28 12
        return $this->id ?? $this->metadata[self::MESSAGE_ID_KEY] ?? null;
29
    }
30
31 12
    protected function getEnvelopeMetadata(): array
32
    {
33 12
        return [self::MESSAGE_ID_KEY => $this->getId()];
34
    }
35
}
36