QueryBuilder::loadColumns()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 8.4751
c 0
b 0
f 0
cc 6
eloc 28
nc 10
nop 0
1
<?php
2
3
namespace AtDataGrid\DataSource\Doctrine;
4
5
use AtDataGrid\DataSource\AbstractDataSource;
6
use AtDataGrid\Column;
7
use AtDataGrid\Filter\Doctrine2Filter;
8
use Doctrine\ORM\EntityManager;
9
use Doctrine\ORM\Tools\Pagination\Paginator;
10
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator;
11
12
class QueryBuilder extends AbstractDataSource
13
{
14
    /**
15
     * @var EntityManager
16
     */
17
    protected $em;
18
19
    /**
20
     * @var \Doctrine\ORM\QueryBuilder
21
     */
22
    protected $qb;
23
24
    /**
25
     * @var string
26
     */
27
    protected $entityName;
28
29
    /**
30
     * @param EntityManager $em
31
     * @param $entityName
32
     */
33
    public function __construct(EntityManager $em, $entityName)
34
    {
35
        $this->em = $em;
36
        $this->qb = $this->em->createQueryBuilder();
37
        $this->entityName = $entityName;
38
        $this->qb->select('f')->from($entityName, 'f');
39
40
        $this->paginatorAdapter = new DoctrinePaginator(new Paginator($this->qb));
0 ignored issues
show
Documentation Bug introduced by
It seems like new \DoctrineORMModule\P...n\Paginator($this->qb)) of type object<DoctrineORMModule...pter\DoctrinePaginator> is incompatible with the declared type object<Zend\Paginator\Adapter\AdapterInterface> of property $paginatorAdapter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
    }
42
43
    /**
44
     * @return EntityManager
45
     */
46
    public function getEntityManager()
47
    {
48
        return $this->em;
49
    }
50
51
    /**
52
     * @return \Doctrine\ORM\QueryBuilder
53
     */
54
    public function getQueryBuilder()
55
    {
56
        return $this->qb;
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function loadColumns()
63
    {
64
        $columns = [];
65
        $classMetadata = $this->em->getClassMetadata($this->entityName);
66
        $baseTableColumns = $classMetadata->getFieldNames();
67
68
        // Setup default settings for base table column fields
69
        foreach ($baseTableColumns as $columnName) {
70
            $columnDataType = $classMetadata->getTypeOfField($columnName);
71
72
            $this->tableColumns[] = $columnName;
0 ignored issues
show
Bug introduced by
The property tableColumns 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...
73
74
            // @todo Move it to separate class
75
            switch (true) {
76
                case in_array($columnDataType, array('datetime', 'timestamp', 'time')):
77
                    $column = new Column\DateTime($columnName);
78
                    break;
79
80
                case in_array($columnDataType, array('date', 'year')):
81
                    $column = new Column\Date($columnName);
82
                    break;
83
84
                case in_array($columnDataType, array('mediumtext', 'text', 'longtext')):
85
                    $column = new Column\Textarea($columnName);
86
                    break;
87
88
                default:
89
                    $column = new Column\Literal($columnName);
90
                    break;
91
            }
92
93
            $column->setLabel($columnName);
94
95
            $columns[$classMetadata->getColumnName($columnName)] = $column;
96
        }
97
98
        $joinedColumns = $classMetadata->getAssociationNames();
99
        foreach ($joinedColumns as $columnName) {
100
            $column = new Column\Literal($classMetadata->getSingleAssociationJoinColumnName($columnName));
101
            $column->setLabel($columnName);
102
103
            $columns[$classMetadata->getSingleAssociationJoinColumnName($columnName)] = $column;
104
        }
105
106
        return $columns;
107
    }
108
109
    /**
110
     * @param $order
111
     * @param array $filters
112
     * @return $this|mixed
113
     */
114
    public function prepare($order, $filters = [])
115
    {
116
        /**
117
         * Filtering
118
         */
119
        foreach ($filters as $columnName => $filter) {
120
            if (!$filter instanceof Doctrine2Filter) {
0 ignored issues
show
Bug introduced by
The class AtDataGrid\Filter\Doctrine2Filter does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
121
                throw new \RuntimeException('Doctrine/QueryBuilder data source requires Filter\Doctrine filters');
122
            }
123
            $filter->apply($this->getQueryBuilder(), $columnName, $filter->getValue());
124
        }
125
126
        /**
127
         * Sorting
128
         */
129
       /* if ($order) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
130
            $orderParts = explode(' ', $order);
131
            if (in_array($orderParts[0], $this->tableColumns)) {
132
                $order = $this->getTableGateway()->getTable() . '.' . $order;
133
            }
134
            $this->getSelect()->order($order);
135
        }*/
136
137
        $this->getEventManager()->trigger(self::EVENT_DATASOURCE_PREPARE_POST, $this->getQueryBuilder());
138
139
        //var_dump($this->getSelect()->getSqlString());exit;
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
140
        return $this;
141
142
    }
143
144
    /**
145
     * Return row by identifier (primary key)
146
     *
147
     * @param $key
148
     * @return array|mixed
149
     */
150
    public function find($key)
151
    {
152
    }
153
154
    /**
155
     * @param $data
156
     * @return int|mixed
157
     */
158
    public function insert($data)
159
    {
160
    }
161
162
    /**
163
     * @param $data
164
     * @param $key
165
     * @return int|mixed
166
     */
167
    public function update($data, $key)
168
    {
169
    }
170
171
    /**
172
     * @param $key
173
     * @return int|mixed
174
     */
175
    public function delete($key)
176
    {
177
    }
178
}