Completed
Pull Request — master (#1569)
by
unknown
03:33
created

ORMPagerProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 8.75 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 7
loc 80
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A provide() 0 38 5

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\Common\Persistence\ManagerRegistry;
15
use Doctrine\ORM\Query\Expr\From;
16
use Doctrine\ORM\QueryBuilder;
17
use FOS\ElasticaBundle\Provider\PagerfantaPager;
18
use FOS\ElasticaBundle\Provider\PagerProviderInterface;
19
use Pagerfanta\Adapter\DoctrineORMAdapter;
20
use Pagerfanta\Pagerfanta;
21
22
final class ORMPagerProvider implements PagerProviderInterface
23
{
24
    const ENTITY_ALIAS = 'a';
25
26
    /**
27
     * @var string
28
     */
29
    private $objectClass;
30
31
    /**
32
     * @var ManagerRegistry
33
     */
34
    private $doctrine;
35
36
    /**
37
     * @var array
38
     */
39
    private $baseOptions;
40
41
    /**
42
     * @var RegisterListenersService
43
     */
44
    private $registerListenersService;
45
46
    /**
47
     * @param ManagerRegistry $doctrine
48
     * @param RegisterListenersService $registerListenersService
49
     * @param string $objectClass
50
     * @param array $baseOptions
51
     */
52 View Code Duplication
    public function __construct(ManagerRegistry $doctrine, RegisterListenersService $registerListenersService, $objectClass, array $baseOptions)
53
    {
54
        $this->doctrine = $doctrine;
55
        $this->objectClass = $objectClass;
56
        $this->baseOptions = $baseOptions;
57
        $this->registerListenersService = $registerListenersService;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function provide(array $options = array())
64
    {
65
        $options = array_replace($this->baseOptions, $options);
66
67
        $manager = $this->doctrine->getManagerForClass($this->objectClass);
68
        $repository = $manager->getRepository($this->objectClass);
69
70
        $qb = \call_user_func([$repository, $options['query_builder_method']], self::ENTITY_ALIAS);
71
72
        // Ensure that the query builder has a sorting configured. Without a ORDER BY clause, the SQL standard does not
73
        // guarantee any order, which breaks the pagination (second page might use a different sorting that when retrieving
74
        // the first page).
75
        // If the QueryBuilder already has its own ordering, or the method returned a Query instead of a QueryBuilder, we
76
        // assume that the query already provides a proper sorting. This allows giving full control over sorting if wanted
77
        // when using a custom method.
78
        if ($qb instanceof QueryBuilder && empty($qb->getDQLPart('orderBy'))) {
79
            // When getting root aliases, the QueryBuilder normalizes all from parts to From objects, in case they were added as string using the low-level API.
80
            // This side-effect allows us to be sure to get only From objects in the next call.
81
            $qb->getRootAliases();
82
83
            /** @var From[] $fromClauses */
84
            $fromClauses = $qb->getDQLPart('from');
85
86
            foreach ($fromClauses as $fromClause) {
87
                $identifiers = $manager->getClassMetadata($fromClause->getFrom())->getIdentifierFieldNames();
88
89
                foreach ($identifiers as $identifier) {
90
                    $qb->addOrderBy($fromClause->getAlias().'.'.$identifier);
91
                }
92
            }
93
        }
94
95
        $pager = new PagerfantaPager(new Pagerfanta(new DoctrineORMAdapter($qb)));
96
97
        $this->registerListenersService->register($manager, $pager, $options);
0 ignored issues
show
Documentation introduced by
$manager is of type object<Doctrine\Persistence\ObjectManager>|null, but the function expects a object<Doctrine\Common\Persistence\ObjectManager>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
99
        return $pager;
100
    }
101
}
102