Completed
Push — master ( eab2a6...645cff )
by Jeroen
30s queued 13s
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
        array $params = array()
110
    ) {
111 2
        $queryBuilder->where('1=1');
112 2
    }
113
114
    /**
115
     * @return int
116
     */
117
    public function getCount()
118
    {
119
        return $this->getPagerfanta()->getNbResults();
120
    }
121
122
    /**
123
     * @return array|mixed|\Traversable
0 ignored issues
show
Consider making the return type a bit more specific; maybe use \Pagerfanta\iterable|null.

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