Passed
Pull Request — master (#1316)
by Alexander
02:53
created

HandlerRegistry   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 93.1%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 26
c 2
b 0
f 1
dl 0
loc 72
ccs 27
cts 29
cp 0.931
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A registerSubscribingHandler() 0 15 5
A registerHandler() 0 3 1
A __construct() 0 3 1
A getDefaultMethod() 0 15 4
A getHandler() 0 7 2
A getHandlers() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Handler;
6
7
use JMS\Serializer\Exception\LogicException;
8
use JMS\Serializer\Exception\RuntimeException;
9
use JMS\Serializer\GraphNavigatorInterface;
10
11
class HandlerRegistry implements HandlerRegistryInterface
12
{
13
    /**
14
     * @var callable[]
15
     */
16 329
    protected $handlers;
17
18 329
    public static function getDefaultMethod(int $direction, string $type, string $format): string
19 284
    {
20
        if (false !== $pos = strrpos($type, '\\')) {
21
            $type = substr($type, $pos + 1);
22
        }
23 329
24 329
        switch ($direction) {
25
            case GraphNavigatorInterface::DIRECTION_DESERIALIZATION:
26 284
                return 'deserialize' . $type . 'From' . $format;
27 284
28
            case GraphNavigatorInterface::DIRECTION_SERIALIZATION:
29
                return 'serialize' . $type . 'To' . $format;
30
31
            default:
32
                throw new LogicException(sprintf('The direction %s does not exist; see GraphNavigatorInterface::DIRECTION_??? constants.', json_encode($direction)));
33
        }
34 338
    }
35
36 338
    public function __construct(array $handlers = [])
37 338
    {
38
        $this->handlers = $handlers;
39 329
    }
40
41 329
    public function registerSubscribingHandler(SubscribingHandlerInterface $handler): void
42 329
    {
43
        foreach ($handler->getSubscribingMethods() as $methodData) {
44
            if (!isset($methodData['type'], $methodData['format'])) {
45
                throw new RuntimeException(sprintf('For each subscribing method a "type" and "format" attribute must be given, but only got "%s" for %s.', implode('" and "', array_keys($methodData)), \get_class($handler)));
46 329
            }
47 329
48 329
            $directions = [GraphNavigatorInterface::DIRECTION_DESERIALIZATION, GraphNavigatorInterface::DIRECTION_SERIALIZATION];
49
            if (isset($methodData['direction'])) {
50
                $directions = [$methodData['direction']];
51 329
            }
52 329
53 329
            foreach ($directions as $direction) {
54
                $method = $methodData['method'] ?? self::getDefaultMethod($direction, $methodData['type'], $methodData['format']);
55
                $this->registerHandler($direction, $methodData['type'], $methodData['format'], [$handler, $method]);
56 329
            }
57
        }
58 334
    }
59
60 334
    /**
61 334
     * {@inheritdoc}
62
     */
63 225
    public function registerHandler(int $direction, string $typeName, string $format, $handler): void
64
    {
65 225
        $this->handlers[$direction][$typeName][$format] = $handler;
66 197
    }
67
68
    /**
69 55
     * {@inheritdoc}
70
     */
71
    public function getHandler(int $direction, string $typeName, string $format)
72
    {
73
        if (!isset($this->handlers[$direction][$typeName][$format])) {
74
            return null;
75
        }
76
77
        return $this->handlers[$direction][$typeName][$format];
78
    }
79
80
    public function getHandlers(): array
81
    {
82
        return $this->handlers;
83
    }
84
}
85