Completed
Push — master ( f9a038...cc9983 )
by Kamil
22:34
created

Driver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\GridBundle\Doctrine\PHPCRODM;
13
14
use Sylius\Component\Grid\Data\DriverInterface;
15
use Sylius\Component\Grid\Parameters;
16
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
17
18
class Driver implements DriverInterface
19
{
20
    /**
21
     * Driver name
22
     */
23
    const NAME = 'doctrine/phpcr-odm';
24
25
    /**
26
     * Alias to use to reference fields from the data source class.
27
     */
28
    const QB_SOURCE_ALIAS = 'o';
29
30
    /**
31
     * @var DocumentManagerInterface
32
     */
33
    private $documentManager;
34
35
    /**
36
     * @param DocumentManagerInterface $documentManager
37
     */
38
    public function __construct(DocumentManagerInterface $documentManager)
39
    {
40
        $this->documentManager = $documentManager;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getDataSource(array $configuration, Parameters $parameters)
47
    {
48
        if (!array_key_exists('class', $configuration)) {
49
            throw new \InvalidArgumentException('"class" must be configured.');
50
        }
51
52
        $repository = $this->documentManager->getRepository($configuration['class']);
53
        $queryBuilder = $repository->createQueryBuilder(self::QB_SOURCE_ALIAS);
54
55
        return new DataSource($queryBuilder);
56
    }
57
}
58