Completed
Push — master ( 3cdfc2...4aa078 )
by Maksim
03:09
created

MongoDBPagerProvider::provide()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
crap 2
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\DoctrineODMMongoDBAdapter;
18
use Pagerfanta\Pagerfanta;
19
20
final class MongoDBPagerProvider implements PagerProviderInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    private $objectClass;
26
27
    /**
28
     * @var ManagerRegistry
29
     */
30
    private $doctrine;
31
    
32
    /**
33
     * @var array
34
     */
35
    private $baseOptions;
36
37
    /**
38
     * @var RegisterListenersService
39
     */
40
    private $registerListenersService;
41
42
    /**
43
     * @param ManagerRegistry $doctrine
44
     * @param RegisterListenersService $registerListenersService
45
     * @param string $objectClass
46
     * @param array $baseOptions
47
     */
48
    public function __construct(ManagerRegistry $doctrine, RegisterListenersService $registerListenersService, $objectClass, array $baseOptions)
49
    {
50
        $this->doctrine = $doctrine;
51
        $this->objectClass = $objectClass;
52
        $this->baseOptions = $baseOptions;
53
        $this->registerListenersService = $registerListenersService;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function provide(array $options = array())
60
    {
61
        $options = array_replace($this->baseOptions, $options);
62
63
        $manager = $this->doctrine->getManagerForClass($this->objectClass);
64
        $repository = $manager->getRepository($this->objectClass);
65
66
        $pager = new PagerfantaPager(new Pagerfanta(
67
            new DoctrineODMMongoDBAdapter(call_user_func([$repository, $options['query_builder_method']]))
68
        ));
69
70
        $this->registerListenersService->register($manager, $pager, $options);
0 ignored issues
show
Bug introduced by
It seems like $manager defined by $this->doctrine->getMana...ass($this->objectClass) on line 63 can be null; however, FOS\ElasticaBundle\Doctr...nersService::register() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
71
72
        return $pager;
73
    }
74
}
75