Completed
Push — master ( e17902...26729b )
by Karel
05:38 queued 11s
created

MongoDBPagerProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 55
Duplicated Lines 40 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 5
dl 22
loc 55
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A provide() 15 15 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\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 4 View Code Duplication
    public function __construct(ManagerRegistry $doctrine, RegisterListenersService $registerListenersService, $objectClass, array $baseOptions)
49
    {
50 4
        $this->doctrine = $doctrine;
51 4
        $this->objectClass = $objectClass;
52 4
        $this->baseOptions = $baseOptions;
53 4
        $this->registerListenersService = $registerListenersService;
54 4
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 3 View Code Duplication
    public function provide(array $options = array())
60
    {
61 3
        $options = array_replace($this->baseOptions, $options);
62
63 3
        $manager = $this->doctrine->getManagerForClass($this->objectClass);
64 3
        $repository = $manager->getRepository($this->objectClass);
65
66 3
        $pager = new PagerfantaPager(new Pagerfanta(
67 3
            new DoctrineODMMongoDBAdapter(call_user_func([$repository, $options['query_builder_method']]))
0 ignored issues
show
Deprecated Code introduced by
The class Pagerfanta\Adapter\DoctrineODMMongoDBAdapter has been deprecated with message: to be removed in 3.0, use the `Pagerfanta\Doctrine\MongoDBODM\QueryAdapter` from the `pagerfanta/doctrine-mongodb-odm-adapter` package instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
68
        ));
69
70 3
        $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 3
        return $pager;
73
    }
74
}
75