Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
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
153
            $this->queryBuilder = new QueryBuilder($this->connection);
154
            $this->adaptQueryBuilder($this->queryBuilder);
155
156
            // Apply filters
157
            $filters = $this->getFilterBuilder()->getCurrentFilters();
158
            foreach ($filters as $filter) {
159
                /* @var AbstractDBALFilterType $type */
160
                $type = $filter->getType();
161
                $type->setQueryBuilder($this->queryBuilder);
162
                $filter->apply();
163
            }
164
165
            // Apply sorting
166 View Code Duplication
            if (!empty($this->orderBy)) {
167
                $orderBy = $this->orderBy;
168
                $this->queryBuilder->orderBy($orderBy, ($this->orderDirection == 'DESC' ? 'DESC' : 'ASC'));
169
            }
170
        }
171
172
        return $this->queryBuilder;
173
    }
174
175
    /**
176
     * Set count field (must include table alias!)
177
     *
178
     * @param string $countField
179
     *
180
     * @return AbstractDoctrineDBALAdminListConfigurator
181
     */
182
    public function setCountField($countField)
183
    {
184
        $this->countField = $countField;
185
186
        return $this;
187
    }
188
189
    /**
190
     * Get current count field (including table alias)
191
     *
192
     * @return string
193
     */
194
    public function getCountField()
195
    {
196
        return $this->countField;
197
    }
198
199
    /**
200
     * When doing the count you can turn the distinct on or off.
201
     *
202
     * @param bool $value
203
     *
204
     * @return AbstractDoctrineDBALAdminListConfigurator
205
     */
206
    public function setUseDistinctCount($value)
207
    {
208
        $this->useDistinctCount = $value;
209
210
        return $this;
211
    }
212
213
    /**
214
     * Get current doDistinctCount
215
     *
216
     * @return bool
217
     */
218
    public function getUseDistinctCount()
219
    {
220
        return $this->useDistinctCount;
221
    }
222
}
223