Test Failed
Push — master ( dc7229...a143ba )
by Mathieu
07:09 queued 10s
created

DatatableWithTotal   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 39
c 1
b 0
f 1
dl 0
loc 94
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A calculTotaux() 0 28 6
A get() 0 10 1
A count() 0 23 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineDatatable;
6
7
/**
8
 * Class DatatableWithTotal.
9
 */
10
abstract class DatatableWithTotal extends Datatable
11
{
12
    /**
13
     * ABSTRACT METHODS.
14
     */
15
16
    /**
17
     * @return string[]
18
     */
19
    abstract protected function getSumableColumns(): array;
20
21
    /**
22
     * PRIVATE METHODS.
23
     */
24
25
    /**
26
     * @return float[]
27
     */
28
    private function calculTotaux(): array
29
    {
30
        $query = clone $this->final_query;
31
32
        $query->resetDQLPart('orderBy')
33
            ->resetDQLPart('groupBy');
34
35
        $index = 0;
36
        foreach ($this->getSumableColumns() as $alias => $column) {
37
            0 == $index ?
38
                $query->select("$column as $alias") :
39
                $query->addSelect("$column as $alias");
40
41
            ++$index;
42
        }
43
44
        $retour = array();
45
46
        foreach ($query->getQuery()->getResult() as $result) {
47
            foreach ($result as $index => $r) {
48
                if (!isset($retour['total_'.$index])) {
49
                    $retour['total_'.$index] = 0;
50
                }
51
                $retour['total_'.$index] += (float) $r;
52
            }
53
        }
54
55
        return $retour;
56
    }
57
58
    /**
59
     * IMPLEMENT METHODS.
60
     */
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function get(array $filters): array
66
    {
67
        $this->createFinalQuery($filters);
68
        $data = $this->data($filters);
69
70
        return array(
71
            'recordsTotal' => $this->count(),
72
            'recordsFiltered' => \count($data),
73
            'data' => $data,
74
            'totalColonnes' => $this->calculTotaux(),
75
        );
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    private function count(): int
82
    {
83
        $tools = new Tools();
84
        $temp = (clone $this->final_query)
85
            ->setFirstResult(0)
86
            ->setMaxResults(null)
87
            ->getQuery();
88
89
        /** @var \Doctrine\ORM\Query\ParserResult $parser */
90
        $parser = $tools->callMethod($temp, '_parse');
91
92
        list($sqlParams, $types) = $tools->callMethod(
93
            $temp,
94
            'processParameterMappings',
95
            array(
96
                $parser->getParameterMappings(),
97
            )
98
        );
99
100
        return (int) ((clone $this->final_query)->getEntityManager()
101
            ->getConnection()
102
            ->executeQuery('SELECT COUNT(*) as total FROM ('.$temp->getSQL().') as t', $sqlParams, $types)
0 ignored issues
show
Bug introduced by
Are you sure $temp->getSQL() of type array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
            ->executeQuery('SELECT COUNT(*) as total FROM ('./** @scrutinizer ignore-type */ $temp->getSQL().') as t', $sqlParams, $types)
Loading history...
103
            ->fetch()['total']);
104
    }
105
}
106