Completed
Pull Request — master (#283)
by
unknown
09:32
created

DoctrineORMAdapter::getNbResults()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 29.1867

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 3
cts 22
cp 0.1364
rs 8.6417
c 0
b 0
f 0
cc 6
nc 5
nop 0
crap 29.1867
1
<?php
2
3
/*
4
 * This file is part of the Pagerfanta package.
5
 *
6
 * (c) Pablo Díez <[email protected]>
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 Pagerfanta\Adapter;
13
14
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
15
use \Doctrine\ORM\EntityManager;
16
17
/**
18
 * DoctrineORMAdapter.
19
 *
20
 * @author Christophe Coevoet <[email protected]>
21
 */
22
class DoctrineORMAdapter implements AdapterInterface
23
{
24
    /**
25
     * @var \Doctrine\ORM\Tools\Pagination\Paginator
26
     */
27
    private $paginator;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param \Doctrine\ORM\Query|\Doctrine\ORM\QueryBuilder $query               A Doctrine ORM query or query builder.
33
     * @param \Doctrine\ORM\EntityManager|null               $em                  Entity Manager
34
     * @param Boolean                                        $fetchJoinCollection Whether the query joins a collection (true by default).
35
     * @param Boolean|null                                   $useOutputWalkers    Whether to use output walkers pagination mode
36
     */
37 8
    public function __construct($query, $em = null, $fetchJoinCollection = true, $useOutputWalkers = null)
38
    {
39 8
        $this->query = $query;
0 ignored issues
show
Bug introduced by
The property query does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40 8
        $this->paginator = new DoctrinePaginator($query, $fetchJoinCollection);
41 8
        $this->paginator->setUseOutputWalkers($useOutputWalkers);
42 8
        $this->em = $em;
0 ignored issues
show
Bug introduced by
The property em does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43 8
    }
44
45
    /**
46
     * Returns the query
47
     *
48
     * @return \Doctrine\ORM\Query
49
     */
50
    public function getQuery()
51
    {
52
        return $this->paginator->getQuery();
53
    }
54
55
    /**
56
     * Returns whether the query joins a collection.
57
     *
58
     * @return Boolean Whether the query joins a collection.
59
     */
60
    public function getFetchJoinCollection()
61
    {
62
        return $this->paginator->getFetchJoinCollection();
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 6
    public function getNbResults()
69
    {
70
        
71 6
        if($this->em === null || empty($this->em)){
72 6
            return count($this->paginator);
73
        } else {
74
        $queryObj = $this->paginator->getQuery();
75
76
//      Pega a query gerada como string sem os par�metros
77
        $sql = $queryObj->getSQL();
78
79
//      Cria e usa fun��o para pegar os par�metros em ordem
80
        $getSqlWithParams = \Closure::bind(function($query){
81
            return [$query->getSql(), $query->processParameterMappings($query->_parserResult->getParameterMappings())];
82
        }, null, \Doctrine\ORM\Query::class);
83
        $qparams = $getSqlWithParams($queryObj);
84
85
//      Percorre pelos par�metros substituindo os '?' na query um por um
86
        foreach ($qparams[1][0] as $value){
87
            if(strpos($sql, '?') !== false){
88
                if(is_array($value)){
89
                    $sql = substr_replace($sql, implode(',', $value), strpos($sql, '?'), 1);
90
                } else {
91
                    $sql = substr_replace($sql, $value, strpos($sql, '?'), 1);
92
                }
93
            }
94
        }
95
96
97
//      Cria a tabela temporaria com  query tratada, faz a contagem da mesma e por fim a exclui
98
        $conn = $conn = $this->em->getConnection();
99
        $sql = 'CREATE TEMPORARY TABLE stage AS ' . $sql;
100
        $conn->executeQuery($sql);
101
        $sql = 'SELECT COUNT(1) FROM stage';
102
        $total = $conn->executeQuery($sql)->fetchColumn(0);
103
        $sql = 'DROP TABLE stage;';
104
        $conn->executeQuery($sql);
105
106
        return $total;
107
        }
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 6
    public function getSlice($offset, $length)
114
    {
115 6
        $this->paginator
116 6
            ->getQuery()
117 6
            ->setFirstResult($offset)
118 6
            ->setMaxResults($length);
119
120 6
        return $this->paginator->getIterator();
121
    }
122
}
123