NoKeyInPayloadException::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Queue\AMQP\Exception;
6
7
use InvalidArgumentException;
8
use Throwable;
9
use Yiisoft\FriendlyException\FriendlyExceptionInterface;
10
use Yiisoft\Queue\AMQP\MessageSerializerInterface;
11
12
class NoKeyInPayloadException extends InvalidArgumentException implements FriendlyExceptionInterface
13
{
14
    public function __construct(
15
        protected string $expectedKey,
16
        protected array $payload,
17
        int $code = 0,
18
        Throwable $previous = null
19
    ) {
20
        parent::__construct(
21
            "No expected key '$expectedKey' in payload. Payload's keys list: " .
22
            implode(', ', array_keys($payload)) .
23
            '.',
24
            $code,
25
            $previous
26
        );
27
    }
28
29
    /**
30
     * @return string
31
     */
32
    public function getName(): string
33
    {
34
        return 'No key "' . $this->expectedKey . '" in payload';
35
    }
36
37
    /**
38
     * @return string
39
     *
40
     * @infection-ignore-all
41
     */
42
    public function getSolution(): ?string
43
    {
44
        return sprintf(
45
            "We have successfully unserialized a message, but there was no expected key \"%s\".
46
        There are the following keys in the message: %s.
47
        You might want to change message's structure, or make your own implementation of %s,
48
        which won't rely on this key in the message.",
49
            $this->expectedKey,
50
            implode(', ', array_keys($this->payload)),
51
            MessageSerializerInterface::class
52
        );
53
    }
54
}
55