Passed
Pull Request — master (#753)
by Maxim
17:58
created

SerializerRegistry::deserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Queue;
6
7
final class SerializerRegistry implements SerializerInterface, SerializerRegistryInterface
8
{
9
    /** @var array<non-empty-string, SerializerInterface> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, SerializerInterface> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, SerializerInterface>.
Loading history...
10
    private array $serializers = [];
11
    private SerializerInterface $default;
12
13
    /** @param array<non-empty-string, SerializerInterface> $serializers */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, SerializerInterface> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, SerializerInterface>.
Loading history...
14 212
    public function __construct(SerializerInterface $default)
15
    {
16 212
        $this->default = $default;
17
    }
18
19 1
    public function serialize(array $payload): string
20
    {
21 1
        return $this->default->serialize($payload);
22
    }
23
24 1
    public function deserialize(string $payload): array
25
    {
26 1
        return $this->default->deserialize($payload);
27
    }
28
29 2
    public function getSerializer(string $jobType): SerializerInterface
30
    {
31 2
        if (!$this->hasSerializer($jobType)) {
32 2
            return $this->default;
33
        }
34
35 2
        return $this->serializers[$jobType];
36
    }
37
38 3
    public function addSerializer(string $jobType, SerializerInterface $serializer): void
39
    {
40 3
        if (!$this->hasSerializer($jobType)) {
41 3
            $this->serializers[$jobType] = $serializer;
42
        }
43
    }
44
45 3
    public function hasSerializer(string $jobType): bool
46
    {
47 3
        return isset($this->serializers[$jobType]);
48
    }
49
}
50