Completed
Push — master ( 770316...74fc07 )
by Jeroen
09:08 queued 02:44
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;
20
21
    /**
22
     * @var QueryBuilder
23
     */
24
    protected $queryBuilder;
25
26
    /**
27
     * @var Pagerfanta
28
     */
29
    private $pagerfanta;
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
        return $this->getQueryBuilder()->execute();
139
    }
140
141
    /**
142
     * @return QueryBuilder|null
143
     */
144 2
    public function getQueryBuilder()
145
    {
146 2
        if (\is_null($this->queryBuilder)) {
147 2
            $this->queryBuilder = new QueryBuilder($this->connection);
148 2
            $this->adaptQueryBuilder($this->queryBuilder);
149
150
            // Apply filters
151 2
            $filters = $this->getFilterBuilder()->getCurrentFilters();
152 2
            foreach ($filters as $filter) {
153
                /* @var AbstractDBALFilterType $type */
154 1
                $type = $filter->getType();
155 1
                $type->setQueryBuilder($this->queryBuilder);
156 1
                $filter->apply();
157
            }
158
159
            // Apply sorting
160 2 View Code Duplication
            if (!empty($this->orderBy)) {
161 1
                $orderBy = $this->orderBy;
162 1
                $this->queryBuilder->orderBy($orderBy, ($this->orderDirection == 'DESC' ? 'DESC' : 'ASC'));
163
            }
164
        }
165
166 2
        return $this->queryBuilder;
167
    }
168
169
    /**
170
     * Set count field (must include table alias!)
171
     *
172
     * @param string $countField
173
     *
174
     * @return AbstractDoctrineDBALAdminListConfigurator
175
     */
176 1
    public function setCountField($countField)
177
    {
178 1
        $this->countField = $countField;
179
180 1
        return $this;
181
    }
182
183
    /**
184
     * Get current count field (including table alias)
185
     *
186
     * @return string
187
     */
188 2
    public function getCountField()
189
    {
190 2
        return $this->countField;
191
    }
192
193
    /**
194
     * When doing the count you can turn the distinct on or off.
195
     *
196
     * @param bool $value
197
     *
198
     * @return AbstractDoctrineDBALAdminListConfigurator
199
     */
200 1
    public function setUseDistinctCount($value)
201
    {
202 1
        $this->useDistinctCount = $value;
203
204 1
        return $this;
205
    }
206
207
    /**
208
     * Get current doDistinctCount
209
     *
210
     * @return bool
211
     */
212 2
    public function getUseDistinctCount()
213
    {
214 2
        return $this->useDistinctCount;
215
    }
216
}
217