Test Failed
Pull Request — master (#217)
by Viktor
02:59
created

AbstractEnvelope::getMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Queue\Message;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
9
abstract class AbstractEnvelope implements EnvelopeInterface
10
{
11
    protected array $metadata = [];
12
    private MessageInterface $message;
13
14
    public function __construct(MessageInterface $message)
15
    {
16
        $this->message = $message instanceof EnvelopeInterface ? $message->getMessage() : $message;
17
        $this->metadata = $message->getMetadata();
18
19
        if (is_array($this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY])) {
20
            $this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY] = array_merge(
21
                [static::class],
22
                array_filter(
23
                    $this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY],
24
                    static fn(string $class) => $class !== static::class,
25
                )
26
            );
27
        } else {
28
            $this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY] = [static::class];
29
        }
30
    }
31
32
    public function getMessage(): MessageInterface
33
    {
34
        return $this->message;
35
    }
36
37
    public function getHandlerName(): string
38
    {
39
        return $this->message->getHandlerName();
40
    }
41
42
    public function getData(): mixed
43
    {
44
        return $this->message->getData();
45
    }
46
47
    public function getMetadata(): array
48
    {
49
        return ArrayHelper::merge(
50
            $this->metadata,
51
            $this->getEnvelopeMetadata(),
52
        );
53
    }
54
55
    /**
56
     * Metadata of the envelope
57
     *
58
     * @return array
59
     */
60
    abstract protected function getEnvelopeMetadata(): array;
61
}
62