Completed
Push — feature/html-code-handler ( e478ba )
by Arnaud
06:02
created

DoctrineRequirementsProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LAG\SmokerBundle\Bridge\Doctrine\ORM\RequirementsProvider;
4
5
use LAG\SmokerBundle\Bridge\Doctrine\ORM\DataProvider\DoctrineDataProviderInterface;
0 ignored issues
show
Bug introduced by
The type LAG\SmokerBundle\Bridge\...neDataProviderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use LAG\SmokerBundle\Exception\Exception;
7
use LAG\SmokerBundle\Exception\Url\UnsupportedUrlException;
8
use LAG\SmokerBundle\Url\Requirements\Mapping\MappingResolverInterface;
9
use LAG\SmokerBundle\Url\Requirements\Provider\RequirementsProviderInterface;
10
use Symfony\Component\PropertyAccess\PropertyAccess;
11
use Symfony\Component\Routing\RouterInterface;
12
use Traversable;
13
14
class DoctrineRequirementsProvider implements RequirementsProviderInterface
15
{
16
    private $name = 'default';
17
18
    /**
19
     * @var RouterInterface
20
     */
21
    protected $router;
22
23
    /**
24
     * @var MappingResolverInterface
25
     */
26
    protected $mappingResolver;
27
28
    /**
29
     * @var DoctrineDataProviderInterface
30
     */
31
    protected $dataProvider;
32
33
    /**
34
     * RequirementsProvider constructor.
35
     *
36
     * @param MappingResolverInterface      $mappingResolver
37
     * @param RouterInterface               $router
38
     * @param DoctrineDataProviderInterface $dataProvider
39
     */
40
    public function __construct(
41
        MappingResolverInterface $mappingResolver,
42
        RouterInterface $router,
43
        DoctrineDataProviderInterface $dataProvider
44
    ) {
45
        $this->mappingResolver = $mappingResolver;
46
        $this->router = $router;
47
        $this->dataProvider = $dataProvider;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getName(): string
54
    {
55
        return $this->name;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function supports(string $routeName): bool
62
    {
63
        return [] !== $this->mappingResolver->resolve($this->name, $routeName);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getRequirements(string $routeName, array $options = []): Traversable
70
    {
71
        $mapping = $this->mappingResolver->resolve($this->name, $routeName);
72
73
        if ([] === $mapping) {
74
            throw new UnsupportedUrlException($routeName, $this->name);
75
        }
76
        $entities = $this
77
            ->dataProvider
78
            ->getData($mapping['entity'], $mapping['options'])
79
        ;
80
        $route = $this
81
            ->router
82
            ->getRouteCollection()
83
            ->get($routeName)
84
        ;
85
        $configuredRequirements = $route->getRequirements();
86
        $accessor = PropertyAccess::createPropertyAccessor();
87
88
        foreach ($entities as $row) {
89
            $entity = $row[0];
90
            $values = [];
91
92
            if (0 === count($configuredRequirements)) {
93
                $matches = [];
94
                preg_match_all('/{(.*?)}/', $route->getPath(), $matches);
95
96
                $configuredRequirements = array_flip($matches[1]);
97
            }
98
99
            foreach ($configuredRequirements as $name => $requirement) {
100
                if (!key_exists($name, $mapping['requirements'])) {
101
                    throw new Exception(sprintf(
102
                        'The requirement "%s" for the route "%s" is not provided',
103
                        $name,
104
                        $routeName
105
                    ));
106
                }
107
                $property = $mapping['requirements'][$name];
108
109
                if ('@' === substr($property, 0, 1)) {
110
                    $values[$name] = substr($property, 1);
111
                } else {
112
                    $values[$name] = $accessor->getValue($entity, $property);
113
                }
114
            }
115
116
            yield $values;
117
        }
118
    }
119
}
120