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

SerializerRegistry   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 41
ccs 15
cts 15
cp 1
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A deserialize() 0 3 1
A addSerializer() 0 4 2
A getSerializer() 0 7 2
A __construct() 0 3 1
A serialize() 0 3 1
A hasSerializer() 0 3 1
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