Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

AbstractDoctrineDBALAdminListConfigurator.php (2 issues)

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

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

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