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

DoctrineDataProvider::getData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LAG\SmokerBundle\Bridge\Doctrine\ORM\DataProvider;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
use Traversable;
9
10
class DoctrineDataProvider implements DoctrineDataProviderInterface
11
{
12
    /**
13
     * @var EntityManagerInterface|EntityManager
14
     */
15
    private $entityManager;
16
17
    public function __construct(EntityManagerInterface $entityManager)
18
    {
19
        $this->entityManager = $entityManager;
20
    }
21
22
    public function getData(string $class, array $options = []): Traversable
23
    {
24
        $options = $this->resolveOptions($options);
25
26
        $queryBuilder = $this
27
            ->entityManager
28
            ->getRepository($class)
29
            ->createQueryBuilder($options['alias'])
30
        ;
31
32
        foreach ($options['where'] as $clause) {
33
            $queryBuilder->andWhere($clause);
34
        }
35
36
        return $queryBuilder->getQuery()->iterate();
37
    }
38
39
    protected function resolveOptions(array $options)
40
    {
41
        $resolver = new OptionsResolver();
42
        $resolver
43
            ->setDefaults([
44
                'alias' => 'entity',
45
                'requirements' => [],
46
                'where' => [],
47
            ])
48
            ->setAllowedTypes('alias', 'string')
49
            ->setAllowedTypes('requirements', 'array')
50
            ->setAllowedTypes('where', 'array')
51
        ;
52
53
        return $resolver->resolve($options);
54
    }
55
}
56