Passed
Pull Request — master (#188)
by Dmitriy
04:23 queued 01:49
created

NoKeyInPayloadException::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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