Passed
Push — main ( ee7b7b...414146 )
by Fractal
02:40
created

fromDiscriminatorMapWhenParameterIsNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Exception;
6
7
use JetBrains\PhpStorm\Pure;
8
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
9
use Throwable;
10
11
final class ClassExtractorException extends \RuntimeException
12
{
13 1
    #[Pure]
14
    public function __construct(private string $property, string $message, int $code = 0, ?Throwable $previous = null)
15
    {
16 1
        parent::__construct($message, $code, $previous);
17 1
    }
18
19
    #[Pure]
20
    public static function fromDiscriminatorMapWhenParameterIsNull(DiscriminatorMap $discriminatorMap): self
21
    {
22
        $property = $discriminatorMap->getTypeProperty();
23
        $message = sprintf('%s cannot be null or empty', $property);
24
25
        return new self($property, $message);
26
    }
27
28 1
    #[Pure]
29
    public static function fromDiscriminatorMapWhenParameterInvalid(DiscriminatorMap $discriminatorMap): self
30
    {
31 1
        $property = $discriminatorMap->getTypeProperty();
32 1
        $expectedValues = array_keys($discriminatorMap->getMapping());
33 1
        $message = sprintf('%s can only be %s', $property, implode(', ', $expectedValues));
34
35 1
        return new self($message, $property);
36
    }
37
38 1
    public function getProperty(): string
39
    {
40 1
        return $this->property;
41
    }
42
}
43