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

NoKeyInPayloadException   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 42
ccs 0
cts 19
cp 0
rs 10
c 1
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A getSolution() 0 10 1
A getName() 0 3 1
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