Completed
Pull Request — master (#16)
by Arnaud
48:07 queued 29:02
created

MappingResolver::resolve()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 11
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 23
rs 8.4444
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
    public function __construct(array $mapping)
23
    {
24
        $this->mapping = $mapping;
25
    }
26
27
    public function resolve(string $routeName, bool $filterMappingData = true): array
28
    {
29
        foreach ($this->mapping as $name => $map) {
30
            $map = $this->resolveMap($map);
31
32
            if (false === $filterMappingData) {
33
                return $map;
34
            }
35
36
            if (in_array($routeName, $map['excludes'])) {
37
                continue;
38
            }
39
40
            if (key_exists('route', $map) && $routeName === $map['route']) {
41
                return $map;
42
            }
43
44
            if (key_exists('pattern', $map) && false !== strpos($routeName, $map['pattern'])) {
45
                return $map;
46
            }
47
        }
48
49
        return [];
50
    }
51
52
    protected function resolveMap(array $map)
53
    {
54
        $resolver = new OptionsResolver();
55
        $resolver
56
            ->setDefaults([
57
                'route' => null,
58
                'pattern' => null,
59
                'requirements' => [],
60
                'excludes' => [],
61
                'provider' => 'doctrine',
62
                'options' => [],
63
            ])
64
            ->setRequired([
65
                'entity',
66
            ])
67
            ->setAllowedTypes('route', [
68
                'string',
69
                'null',
70
            ])
71
            ->setAllowedTypes('pattern', [
72
                'string',
73
                'null',
74
            ])
75
            ->setAllowedTypes('provider', 'string')
76
            ->setAllowedTypes('entity', 'string')
77
            ->setAllowedTypes('requirements', 'array')
78
            ->setAllowedTypes('options', 'array')
79
            ->setNormalizer('route', function (Options $options, $value) {
80
                if (null === $value && null === $options->offsetGet('pattern')) {
81
                    throw new InvalidOptionsException('A pattern or a route should be provided');
82
                }
83
84
                return $value;
85
            });
86
87
        return $resolver->resolve($map);
88
    }
89
}
90