Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

AbstractDoctrineDBALAdminListConfigurator.php (1 issue)

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 6
    public function __construct(Connection $connection)
45
    {
46 6
        $this->connection = $connection;
47 6
    }
48
49
    /**
50
     * Return the url to edit the given $item
51
     *
52
     * @param array $item
53
     *
54
     * @return array
55
     */
56 1 View Code Duplication
    public function getEditUrlFor($item)
57
    {
58 1
        $params = array('id' => $item['id']);
59 1
        $params = array_merge($params, $this->getExtraParameters());
60
61
        return array(
62 1
            'path' => $this->getPathByConvention($this::SUFFIX_EDIT),
63 1
            '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 1 View Code Duplication
    public function getDeleteUrlFor($item)
75
    {
76 1
        $params = array('id' => $item['id']);
77 1
        $params = array_merge($params, $this->getExtraParameters());
78
79
        return array(
80 1
            'path' => $this->getPathByConvention($this::SUFFIX_DELETE),
81 1
            'params' => $params,
82
        );
83
    }
84
85
    /**
86
     * @return Pagerfanta
87
     */
88 1 View Code Duplication
    public function getPagerfanta()
89
    {
90 1
        if (is_null($this->pagerfanta)) {
91 1
            $adapter = new DoctrineDBALAdapter(
92 1
                $this->getQueryBuilder(),
93 1
                $this->getCountField(),
94 1
                $this->getUseDistinctCount()
95
            );
96 1
            $this->pagerfanta = new Pagerfanta($adapter);
97 1
            $this->pagerfanta->setMaxPerPage($this->getLimit());
98 1
            $this->pagerfanta->setCurrentPage($this->getPage());
99
        }
100
101 1
        return $this->pagerfanta;
102
    }
103
104
    /**
105
     * @param array $params
106
     */
107 2
    public function adaptQueryBuilder(
108
        QueryBuilder $queryBuilder,
109
        /* @noinspection PhpUnusedParameterInspection */
110
        array $params = array()
111
    ) {
112 2
        $queryBuilder->where('1=1');
113 2
    }
114
115
    /**
116
     * @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...
117
     */
118
    public function getCount()
119
    {
120
        return $this->getPagerfanta()->getNbResults();
121
    }
122
123
    /**
124
     * @return array|mixed|\Traversable
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 1
    public function getIterator()
137
    {
138 1
        $statement = $this->getQueryBuilder()->execute();
139
140 1
        return $statement;
141
    }
142
143
    /**
144
     * @return QueryBuilder|null
145
     */
146 2
    public function getQueryBuilder()
147
    {
148 2
        if (is_null($this->queryBuilder)) {
149 2
            $this->queryBuilder = new QueryBuilder($this->connection);
150 2
            $this->adaptQueryBuilder($this->queryBuilder);
151
152
            // Apply filters
153 2
            $filters = $this->getFilterBuilder()->getCurrentFilters();
154 2
            foreach ($filters as $filter) {
155
                /* @var AbstractDBALFilterType $type */
156 1
                $type = $filter->getType();
157 1
                $type->setQueryBuilder($this->queryBuilder);
158 1
                $filter->apply();
159
            }
160
161
            // Apply sorting
162 2 View Code Duplication
            if (!empty($this->orderBy)) {
163 1
                $orderBy = $this->orderBy;
164 1
                $this->queryBuilder->orderBy($orderBy, ($this->orderDirection == 'DESC' ? 'DESC' : 'ASC'));
165
            }
166
        }
167
168 2
        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 1
    public function setCountField($countField)
179
    {
180 1
        $this->countField = $countField;
181
182 1
        return $this;
183
    }
184
185
    /**
186
     * Get current count field (including table alias)
187
     *
188
     * @return string
189
     */
190 2
    public function getCountField()
191
    {
192 2
        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 1
    public function setUseDistinctCount($value)
203
    {
204 1
        $this->useDistinctCount = $value;
205
206 1
        return $this;
207
    }
208
209
    /**
210
     * Get current doDistinctCount
211
     *
212
     * @return bool
213
     */
214 2
    public function getUseDistinctCount()
215
    {
216 2
        return $this->useDistinctCount;
217
    }
218
}
219