Passed
Push — master ( 01b790...988c53 )
by Jan
04:36
created

CustomORMAdapter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getResults() 0 25 5
A prepareQuery() 0 4 1
A configure() 0 4 2
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 */
21
22
namespace App\DataTables\Adapter;
23
24
25
use Doctrine\ORM\Query;
26
use Doctrine\ORM\QueryBuilder;
27
use Omines\DataTablesBundle\Adapter\AdapterQuery;
28
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
29
use Omines\DataTablesBundle\Column\AbstractColumn;
30
31
/**
32
 * Override default ORM Adapter, to allow fetch joins (allow addSelect with ManyToOne Collections).
33
 * This should improves performance for Part Tables.
34
 * Based on: https://github.com/omines/datatables-bundle/blob/master/tests/Fixtures/AppBundle/DataTable/Adapter/CustomORMAdapter.php
35
 * @package App\DataTables\Adapter
36
 */
37
class CustomORMAdapter extends ORMAdapter
38
{
39
    protected $hydrationMode;
40
    public function configure(array $options)
41
    {
42
        parent::configure($options);
43
        $this->hydrationMode = isset($options['hydrate']) ? $options['hydrate'] : Query::HYDRATE_OBJECT;
44
    }
45
    protected function prepareQuery(AdapterQuery $query)
46
    {
47
        parent::prepareQuery($query);
48
        $query->setIdentifierPropertyPath(null);
49
    }
50
    /**
51
     * @param AdapterQuery $query
52
     * @return \Traversable
53
     */
54
    protected function getResults(AdapterQuery $query): \Traversable
55
    {
56
        /** @var QueryBuilder $builder */
57
        $builder = $query->get('qb');
58
        $state = $query->getState();
59
        // Apply definitive view state for current 'page' of the table
60
        foreach ($state->getOrderBy() as list($column, $direction)) {
61
            /** @var AbstractColumn $column */
62
            if ($column->isOrderable()) {
63
                $builder->addOrderBy($column->getOrderField(), $direction);
64
            }
65
        }
66
        if ($state->getLength() > 0) {
67
            $builder
68
                ->setFirstResult($state->getStart())
69
                ->setMaxResults($state->getLength());
70
        }
71
        /*
72
         * Use foreach instead of iterate to prevent group by from crashing
73
         */
74
        foreach ($builder->getQuery()->getResult($this->hydrationMode) as $result) {
75
            /*
76
             * Return everything instead of first element
77
             */
78
            yield $result;
79
        }
80
    }
81
}