Completed
Push — master ( 1db3cd...632e40 )
by Jeroen
24:52 queued 11:31
created

AbstractDoctrineDBALAdminListConfigurator.php (1 issue)

Checks whether return doc types can be made more specific.

Documentation Informational

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminListBundle\AdminList\Configurator;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Query\QueryBuilder;
7
use Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL\AbstractDBALFilterType;
8
use Kunstmaan\AdminListBundle\Helper\DoctrineDBALAdapter;
9
use Pagerfanta\Pagerfanta;
10
11
/**
12
 * An abstract admin list configurator that can be used with dbal query builder
13
 */
14
abstract class AbstractDoctrineDBALAdminListConfigurator extends AbstractAdminListConfigurator
15
{
16
    /**
17
     * @var Connection
18
     */
19
    protected $connection = null;
20
21
    /**
22
     * @var QueryBuilder
23
     */
24
    protected $queryBuilder = null;
25
26
    /**
27
     * @var Pagerfanta
28
     */
29
    private $pagerfanta = null;
30
31
    /**
32
     * @var string
33
     */
34
    private $countField = 'b.id';
35
36
    /**
37
     * @var bool
38
     */
39
    private $useDistinctCount = true;
40
41
    /**
42
     * @param Connection $connection
43
     */
44
    public function __construct(Connection $connection)
45
    {
46
        $this->connection = $connection;
47
    }
48
49
    /**
50
     * Return the url to edit the given $item
51
     *
52
     * @param array $item
53
     *
54
     * @return array
55
     */
56 View Code Duplication
    public function getEditUrlFor($item)
57
    {
58
        $params = array('id' => $item['id']);
59
        $params = array_merge($params, $this->getExtraParameters());
60
61
        return array(
62
            'path' => $this->getPathByConvention($this::SUFFIX_EDIT),
63
            'params' => $params,
64
        );
65
    }
66
67
    /**
68
     * Get the delete url for the given $item
69
     *
70
     * @param array $item
71
     *
72
     * @return array
73
     */
74 View Code Duplication
    public function getDeleteUrlFor($item)
75
    {
76
        $params = array('id' => $item['id']);
77
        $params = array_merge($params, $this->getExtraParameters());
78
79
        return array(
80
            'path' => $this->getPathByConvention($this::SUFFIX_DELETE),
81
            'params' => $params,
82
        );
83
    }
84
85
    /**
86
     * @return Pagerfanta
87
     */
88 View Code Duplication
    public function getPagerfanta()
89
    {
90
        if (is_null($this->pagerfanta)) {
91
            $adapter = new DoctrineDBALAdapter(
92
                $this->getQueryBuilder(),
93
                $this->getCountField(),
94
                $this->getUseDistinctCount()
95
            );
96
            $this->pagerfanta = new Pagerfanta($adapter);
97
            $this->pagerfanta->setMaxPerPage($this->getLimit());
98
            $this->pagerfanta->setCurrentPage($this->getPage());
99
        }
100
101
        return $this->pagerfanta;
102
    }
103
104
    /**
105
     * @param array $params
106
     */
107
    public function adaptQueryBuilder(
108
        QueryBuilder $queryBuilder,
109
        /* @noinspection PhpUnusedParameterInspection */
110
        array $params = array()
111
    ) {
112
        $queryBuilder->where('1=1');
113
    }
114
115
    /**
116
     * @return int
117
     */
118
    public function getCount()
119
    {
120
        return $this->getPagerfanta()->getNbResults();
121
    }
122
123
    /**
124
     * @return array|mixed|\Traversable
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array|\Traversable.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
125
     */
126
    public function getItems()
127
    {
128
        return $this->getPagerfanta()->getCurrentPageResults();
129
    }
130
131
    /**
132
     * Return an iterable statement or int for all items that matches the current filtering
133
     *
134
     * @return \Traversable|int
135
     */
136
    public function getIterator()
137
    {
138
        $statement = $this->getQueryBuilder()->execute();
139
140
        return $statement;
141
    }
142
143
    /**
144
     * @return QueryBuilder|null
145
     */
146
    public function getQueryBuilder()
147
    {
148
        if (is_null($this->queryBuilder)) {
149
            $this->queryBuilder = new QueryBuilder($this->connection);
150
            $this->adaptQueryBuilder($this->queryBuilder);
151
152
            // Apply filters
153
            $filters = $this->getFilterBuilder()->getCurrentFilters();
154
            foreach ($filters as $filter) {
155
                /* @var AbstractDBALFilterType $type */
156
                $type = $filter->getType();
157
                $type->setQueryBuilder($this->queryBuilder);
158
                $filter->apply();
159
            }
160
161
            // Apply sorting
162 View Code Duplication
            if (!empty($this->orderBy)) {
163
                $orderBy = $this->orderBy;
164
                $this->queryBuilder->orderBy($orderBy, ($this->orderDirection == 'DESC' ? 'DESC' : 'ASC'));
165
            }
166
        }
167
168
        return $this->queryBuilder;
169
    }
170
171
    /**
172
     * Set count field (must include table alias!)
173
     *
174
     * @param string $countField
175
     *
176
     * @return AbstractDoctrineDBALAdminListConfigurator
177
     */
178
    public function setCountField($countField)
179
    {
180
        $this->countField = $countField;
181
182
        return $this;
183
    }
184
185
    /**
186
     * Get current count field (including table alias)
187
     *
188
     * @return string
189
     */
190
    public function getCountField()
191
    {
192
        return $this->countField;
193
    }
194
195
    /**
196
     * When doing the count you can turn the distinct on or off.
197
     *
198
     * @param bool $value
199
     *
200
     * @return AbstractDoctrineDBALAdminListConfigurator
201
     */
202
    public function setUseDistinctCount($value)
203
    {
204
        $this->useDistinctCount = $value;
205
206
        return $this;
207
    }
208
209
    /**
210
     * Get current doDistinctCount
211
     *
212
     * @return bool
213
     */
214
    public function getUseDistinctCount()
215
    {
216
        return $this->useDistinctCount;
217
    }
218
}
219