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

AbstractEnvelope   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 52
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A getMessage() 0 3 1
A getHandlerName() 0 3 1
A getData() 0 3 1
A getMetadata() 0 5 1
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