ResourceSerializerResolver::resolveByMediaType()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the FiveLab Resource package
7
 *
8
 * (c) FiveLab
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code
12
 */
13
14
namespace FiveLab\Component\Resource\Serializer\Resolver;
15
16
use FiveLab\Component\Resource\Serializer\ResourceSerializerInterface;
17
18
/**
19
 * Default resolver for resolve serializer by media types.
20
 *
21
 * @author Vitaliy Zhuk <[email protected]>
22
 */
23
class ResourceSerializerResolver implements ResourceSerializerResolverInterface
24
{
25
    /**
26
     * @var array
27
     */
28
    private $map = [];
29
30
    /**
31
     * Add the resource normalizer to registry
32
     *
33
     * @param ResourceSerializerSupportableInterface $supportable
34
     * @param ResourceSerializerInterface            $serializer
35
     */
36 2
    public function add(ResourceSerializerSupportableInterface $supportable, ResourceSerializerInterface $serializer): void
37
    {
38 2
        $this->map[] = [$supportable, $serializer];
39 2
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 4
    public function resolveByMediaType(string $resourceClass, string $mediaType): ResourceSerializerInterface
45
    {
46
        /** @var ResourceSerializerSupportableInterface $supportable */
47 4
        foreach ($this->map as [$supportable, $serializer]) {
48 2
            if ($supportable->supports($resourceClass, $mediaType)) {
0 ignored issues
show
Bug introduced by
The variable $supportable does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
49 2
                return $serializer;
0 ignored issues
show
Bug introduced by
The variable $serializer does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
50
            }
51
        }
52
53 3
        throw new ResourceSerializerNotFoundException(\sprintf(
54 3
            'Not found serializer for resource for media type "%s".',
55 3
            $mediaType
56
        ));
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 2
    public function resolveByMediaTypes(string $resourceClass, array $mediaTypes, &$acceptMediaType): ResourceSerializerInterface
63
    {
64 2
        $serializer = null;
65
66 2
        foreach ($mediaTypes as $mediaType) {
67
            try {
68 2
                $serializer = $this->resolveByMediaType($resourceClass, $mediaType);
69 1
                $acceptMediaType = $mediaType;
70
71 1
                break;
72 2
            } catch (ResourceSerializerNotFoundException $e) {
73
                // The media type not supported.
74
            }
75
        }
76
77 2
        if (!$serializer) {
78 1
            throw new ResourceSerializerNotFoundException(\sprintf(
79 1
                'Can\'t resolve resource serializer for any media types: "%s".',
80 1
                \implode('", "', $mediaTypes)
81
            ));
82
        }
83
84 1
        return $serializer;
85
    }
86
}
87