MappingResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace LAG\SmokerBundle\Url\Requirements\Mapping;
4
5
use LAG\SmokerBundle\Contracts\Requirements\Mapping\MappingResolverInterface;
6
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
7
use Symfony\Component\OptionsResolver\Options;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
/**
11
 * This class resolve the mapping configuration for a given provider. The mapping of url requirements are
12
 * based on entities and not providers themselves, so we must loop on each mapping to find data related
13
 * to the given provider.
14
 */
15
class MappingResolver implements MappingResolverInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    private $mapping;
21
22 4
    public function __construct(array $mapping)
23
    {
24 4
        $this->mapping = $mapping;
25 4
    }
26
27 4
    public function resolve(string $routeName, bool $filterMappingData = true): array
28
    {
29 4
        foreach ($this->mapping as $name => $map) {
30 3
            $map = $this->resolveMap($map);
31
32 2
            if (false === $filterMappingData) {
33
                return $map;
34
            }
35
36 2
            if (in_array($routeName, $map['excludes'])) {
37
                continue;
38
            }
39
40 2
            if (key_exists('route', $map) && $routeName === $map['route']) {
41
                return $map;
42
            }
43
44 2
            if (key_exists('pattern', $map) && false !== strpos($routeName, $map['pattern'])) {
45
                return $map;
46
            }
47
        }
48
49 3
        return [];
50
    }
51
52 3
    protected function resolveMap(array $map)
53
    {
54 3
        $resolver = new OptionsResolver();
55
        $resolver
56 3
            ->setDefaults([
57 3
                'route' => null,
58
                'pattern' => null,
59
                'requirements' => [],
60
                'excludes' => [],
61
                'provider' => 'doctrine',
62
                'options' => [],
63
            ])
64 3
            ->setRequired([
65 3
                'entity',
66
            ])
67 3
            ->setAllowedTypes('route', [
68 3
                'string',
69
                'null',
70
            ])
71 3
            ->setAllowedTypes('pattern', [
72 3
                'string',
73
                'null',
74
            ])
75 3
            ->setAllowedTypes('provider', 'string')
76 3
            ->setAllowedTypes('entity', 'string')
77 3
            ->setAllowedTypes('requirements', 'array')
78 3
            ->setAllowedTypes('options', 'array')
79
            ->setNormalizer('route', function (Options $options, $value) {
80 2
                if (null === $value && null === $options->offsetGet('pattern')) {
81
                    throw new InvalidOptionsException('A pattern or a route should be provided');
82
                }
83
84 2
                return $value;
85 3
            });
86
87 3
        return $resolver->resolve($map);
88
    }
89
}
90