Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
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 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 6
    public function __construct(Connection $connection)
42
    {
43 6
        $this->connection = $connection;
44 6
    }
45
46
    /**
47
     * Return the url to edit the given $item
48
     *
49
     * @param array $item
50
     *
51
     * @return array
52
     */
53 1 View Code Duplication
    public function getEditUrlFor($item)
54
    {
55 1
        $params = ['id' => $item['id']];
56 1
        $params = array_merge($params, $this->getExtraParameters());
57
58
        return [
59 1
            'path' => $this->getPathByConvention($this::SUFFIX_EDIT),
60 1
            'params' => $params,
61
        ];
62
    }
63
64
    /**
65
     * Get the delete url for the given $item
66
     *
67
     * @param array $item
68
     *
69
     * @return array
70
     */
71 1 View Code Duplication
    public function getDeleteUrlFor($item)
72
    {
73 1
        $params = ['id' => $item['id']];
74 1
        $params = array_merge($params, $this->getExtraParameters());
75
76
        return [
77 1
            'path' => $this->getPathByConvention($this::SUFFIX_DELETE),
78 1
            'params' => $params,
79
        ];
80
    }
81
82
    /**
83
     * @return Pagerfanta
84
     */
85 1 View Code Duplication
    public function getPagerfanta()
86
    {
87 1
        if (\is_null($this->pagerfanta)) {
88 1
            $adapter = new DoctrineDBALAdapter(
89 1
                $this->getQueryBuilder(),
0 ignored issues
show
It seems like $this->getQueryBuilder() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
90 1
                $this->getCountField(),
91 1
                $this->getUseDistinctCount()
92
            );
93 1
            $this->pagerfanta = new Pagerfanta($adapter);
94 1
            $this->pagerfanta->setMaxPerPage($this->getLimit());
95 1
            $this->pagerfanta->setCurrentPage($this->getPage());
96
        }
97
98 1
        return $this->pagerfanta;
99
    }
100
101 2
    public function adaptQueryBuilder(
102
        QueryBuilder $queryBuilder,
103
        array $params = []
104
    ) {
105 2
        $queryBuilder->where('1=1');
106 2
    }
107
108
    /**
109
     * @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...
110
     */
111
    public function getCount()
112
    {
113
        return $this->getPagerfanta()->getNbResults();
114
    }
115
116
    /**
117
     * @return array|mixed|\Traversable
118
     */
119
    public function getItems()
120
    {
121
        return $this->getPagerfanta()->getCurrentPageResults();
122
    }
123
124
    /**
125
     * Return an iterable statement or int for all items that matches the current filtering
126
     *
127
     * @return \Traversable|int
128
     */
129 1
    public function getIterator()
130
    {
131 1
        return $this->getQueryBuilder()->execute();
132
    }
133
134
    /**
135
     * @return QueryBuilder|null
136
     */
137 2
    public function getQueryBuilder()
138
    {
139 2
        if (\is_null($this->queryBuilder)) {
140 2
            $this->queryBuilder = new QueryBuilder($this->connection);
141 2
            $this->adaptQueryBuilder($this->queryBuilder);
142
143
            // Apply filters
144 2
            $filters = $this->getFilterBuilder()->getCurrentFilters();
145 2
            foreach ($filters as $filter) {
146
                /* @var AbstractDBALFilterType $type */
147 1
                $type = $filter->getType();
148 1
                $type->setQueryBuilder($this->queryBuilder);
149 1
                $filter->apply();
150
            }
151
152
            // Apply sorting
153 2 View Code Duplication
            if (!empty($this->orderBy)) {
154 1
                $orderBy = $this->orderBy;
155 1
                $this->queryBuilder->orderBy($orderBy, ($this->orderDirection == 'DESC' ? 'DESC' : 'ASC'));
156
            }
157
        }
158
159 2
        return $this->queryBuilder;
160
    }
161
162
    /**
163
     * Set count field (must include table alias!)
164
     *
165
     * @param string $countField
166
     *
167
     * @return AbstractDoctrineDBALAdminListConfigurator
168
     */
169 1
    public function setCountField($countField)
170
    {
171 1
        $this->countField = $countField;
172
173 1
        return $this;
174
    }
175
176
    /**
177
     * Get current count field (including table alias)
178
     *
179
     * @return string
180
     */
181 2
    public function getCountField()
182
    {
183 2
        return $this->countField;
184
    }
185
186
    /**
187
     * When doing the count you can turn the distinct on or off.
188
     *
189
     * @param bool $value
190
     *
191
     * @return AbstractDoctrineDBALAdminListConfigurator
192
     */
193 1
    public function setUseDistinctCount($value)
194
    {
195 1
        $this->useDistinctCount = $value;
196
197 1
        return $this;
198
    }
199
200
    /**
201
     * Get current doDistinctCount
202
     *
203
     * @return bool
204
     */
205 2
    public function getUseDistinctCount()
206
    {
207 2
        return $this->useDistinctCount;
208
    }
209
}
210