CallbackLinkNormalizer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 16
dl 0
loc 44
ccs 15
cts 15
cp 1
rs 10
c 1
b 0
f 1
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeLink() 0 25 4
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Normalizer;
6
7
use Chubbyphp\Serialization\Policy\PolicyInterface;
8
use Chubbyphp\Serialization\SerializerLogicException;
9
use Psr\Link\LinkInterface;
10
11
final class CallbackLinkNormalizer implements LinkNormalizerInterface
12
{
13
    /**
14
     * @var callable
15
     */
16
    private $callback;
17
18
    public function __construct(callable $callback)
19
    {
20
        $this->callback = $callback;
21 3
    }
22
23 3
    /**
24 3
     * @param object $object
25
     *
26
     * @throws SerializerLogicException
27
     *
28
     * @return array|null
29
     */
30
    public function normalizeLink(string $path, $object, NormalizerContextInterface $context)
31
    {
32
        $callback = $this->callback;
33
34
        $link = $callback($path, $object, $context);
35 3
36
        if (null === $link) {
37 3
            @trigger_error(
38
                sprintf(
39 3
                    'If a link should not be serialized under certain conditions, use a "%s" instead',
40
                    PolicyInterface::class
41 3
                ),
42 1
                E_USER_DEPRECATED
43 1
            );
44 1
45 1
            return null;
46
        }
47 1
48
        if (!$link instanceof LinkInterface) {
49
            $type = is_object($link) ? get_class($link) : gettype($link);
50 1
51
            throw SerializerLogicException::createInvalidLinkTypeReturned($path, $type);
52
        }
53 2
54 1
        return (new LinkNormalizer($link))->normalizeLink($path, $object, $context);
55
    }
56
}
57