Completed
Push — master ( 4e4cd8...063296 )
by Maksim
16s
created

PHPCRPagerProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 54
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A provide() 0 8 1
A createQueryBuilder() 0 7 1
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\ElasticaBundle\Doctrine;
13
14
use Doctrine\Common\Persistence\ManagerRegistry;
15
use FOS\ElasticaBundle\Provider\PagerfantaPager;
16
use FOS\ElasticaBundle\Provider\PagerProviderInterface;
17
use Pagerfanta\Adapter\DoctrineODMPhpcrAdapter;
18
use Pagerfanta\Pagerfanta;
19
20
final class PHPCRPagerProvider implements PagerProviderInterface
21
{
22
    const ENTITY_ALIAS = 'a';
23
24
    /**
25
     * @var string
26
     */
27
    private $objectClass;
28
29
    /**
30
     * @var ManagerRegistry
31
     */
32
    private $doctrine;
33
34
    /**
35
     * @var array
36
     */
37
    private $baseOptions;
38
39
    /**
40
     * @param ManagerRegistry $doctrine
41
     * @param string          $objectClass
42
     * @param array           $baseOptions
43
     */
44 3
    public function __construct(ManagerRegistry $doctrine, $objectClass, array $baseOptions)
45
    {
46 3
        $this->doctrine = $doctrine;
47 3
        $this->objectClass = $objectClass;
48 3
        $this->baseOptions = $baseOptions;
49 3
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 2
    public function provide(array $options = array())
55
    {
56 2
        $options = array_replace($this->baseOptions, $options);
57
        
58 2
        $adapter = new DoctrineODMPhpcrAdapter($this->createQueryBuilder($options['query_builder_method']));
59
60 2
        return new PagerfantaPager(new Pagerfanta($adapter));
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 2
    protected function createQueryBuilder($method)
67
    {
68 2
        return $this->doctrine
69 2
            ->getManagerForClass($this->objectClass)
70 2
            ->getRepository($this->objectClass)
71 2
            ->{$method}(static::ENTITY_ALIAS);
72
    }
73
}
74