ResourceAssemblerResolver::resolve()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.7666
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\Assembler\Resolver;
15
16
use FiveLab\Component\Resource\Assembler\ResourceAssemblerInterface;
17
18
/**
19
 * Resolve resource assembler.
20
 *
21
 * @author Vitaliy Zhuk <[email protected]>
22
 */
23
class ResourceAssemblerResolver implements ResourceAssemblerResolverInterface
24
{
25
    /**
26
     * @var array
27
     */
28
    private $map = [];
29
30
    /**
31
     * Add the resource assembler to registry
32
     *
33
     * @param ResourceAssemblerSupportableInterface $supportable
34
     * @param ResourceAssemblerInterface            $assembler
35
     */
36 1
    public function add(ResourceAssemblerSupportableInterface $supportable, ResourceAssemblerInterface $assembler): void
37
    {
38 1
        $this->map[] = [$supportable, $assembler];
39 1
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 2
    public function resolve(string $objectClass, string $resourceClass): ResourceAssemblerInterface
45
    {
46
        /** @var ResourceAssemblerSupportableInterface $supportable */
47 2
        foreach ($this->map as [$supportable, $assembler]) {
48 1
            if ($supportable->supports($objectClass, $resourceClass)) {
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 1
                return $assembler;
0 ignored issues
show
Bug introduced by
The variable $assembler 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 1
        throw new ResourceAssemblerNotFoundException(sprintf(
54 1
            'Not found assembler for resource by class "%s" and object by class "%s".',
55 1
            $resourceClass,
56 1
            $objectClass
57
        ));
58
    }
59
}
60