Completed
Push — master ( 1db3cd...632e40 )
by Jeroen
24:52 queued 11:31
created

AbstractDoctrineDBALAdminListConfigurator   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 205
Duplicated Lines 19.02 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 6
dl 39
loc 205
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getEditUrlFor() 10 10 1
A getDeleteUrlFor() 10 10 1
A getPagerfanta() 15 15 2
A adaptQueryBuilder() 0 7 1
A getCount() 0 4 1
A getItems() 0 4 1
A getIterator() 0 6 1
A getQueryBuilder() 4 24 5
A setCountField() 0 6 1
A getCountField() 0 4 1
A setUseDistinctCount() 0 6 1
A getUseDistinctCount() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public function __construct(Connection $connection)
45
    {
46
        $this->connection = $connection;
47
    }
48
49
    /**
50
     * Return the url to edit the given $item
51
     *
52
     * @param array $item
53
     *
54
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string|array>.

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...
55
     */
56 View Code Duplication
    public function getEditUrlFor($item)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        $params = array('id' => $item['id']);
59
        $params = array_merge($params, $this->getExtraParameters());
60
61
        return array(
62
            'path' => $this->getPathByConvention($this::SUFFIX_EDIT),
63
            'params' => $params,
64
        );
65
    }
66
67
    /**
68
     * Get the delete url for the given $item
69
     *
70
     * @param array $item
71
     *
72
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string|array>.

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...
73
     */
74 View Code Duplication
    public function getDeleteUrlFor($item)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $params = array('id' => $item['id']);
77
        $params = array_merge($params, $this->getExtraParameters());
78
79
        return array(
80
            'path' => $this->getPathByConvention($this::SUFFIX_DELETE),
81
            'params' => $params,
82
        );
83
    }
84
85
    /**
86
     * @return Pagerfanta
87
     */
88 View Code Duplication
    public function getPagerfanta()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        if (is_null($this->pagerfanta)) {
91
            $adapter = new DoctrineDBALAdapter(
92
                $this->getQueryBuilder(),
0 ignored issues
show
Bug introduced by
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...
93
                $this->getCountField(),
94
                $this->getUseDistinctCount()
95
            );
96
            $this->pagerfanta = new Pagerfanta($adapter);
97
            $this->pagerfanta->setMaxPerPage($this->getLimit());
98
            $this->pagerfanta->setCurrentPage($this->getPage());
99
        }
100
101
        return $this->pagerfanta;
102
    }
103
104
    /**
105
     * @param array $params
106
     */
107
    public function adaptQueryBuilder(
108
        QueryBuilder $queryBuilder,
109
        /* @noinspection PhpUnusedParameterInspection */
110
        array $params = array()
111
    ) {
112
        $queryBuilder->where('1=1');
113
    }
114
115
    /**
116
     * @return int
0 ignored issues
show
Documentation introduced by
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
0 ignored issues
show
Documentation introduced by
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...
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
    public function getIterator()
137
    {
138
        $statement = $this->getQueryBuilder()->execute();
139
140
        return $statement;
141
    }
142
143
    /**
144
     * @return QueryBuilder|null
145
     */
146
    public function getQueryBuilder()
147
    {
148
        if (is_null($this->queryBuilder)) {
149
            $this->queryBuilder = new QueryBuilder($this->connection);
150
            $this->adaptQueryBuilder($this->queryBuilder);
151
152
            // Apply filters
153
            $filters = $this->getFilterBuilder()->getCurrentFilters();
154
            foreach ($filters as $filter) {
155
                /* @var AbstractDBALFilterType $type */
156
                $type = $filter->getType();
157
                $type->setQueryBuilder($this->queryBuilder);
158
                $filter->apply();
159
            }
160
161
            // Apply sorting
162 View Code Duplication
            if (!empty($this->orderBy)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
                $orderBy = $this->orderBy;
164
                $this->queryBuilder->orderBy($orderBy, ($this->orderDirection == 'DESC' ? 'DESC' : 'ASC'));
165
            }
166
        }
167
168
        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
    public function setCountField($countField)
179
    {
180
        $this->countField = $countField;
181
182
        return $this;
183
    }
184
185
    /**
186
     * Get current count field (including table alias)
187
     *
188
     * @return string
189
     */
190
    public function getCountField()
191
    {
192
        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
    public function setUseDistinctCount($value)
203
    {
204
        $this->useDistinctCount = $value;
205
206
        return $this;
207
    }
208
209
    /**
210
     * Get current doDistinctCount
211
     *
212
     * @return bool
213
     */
214
    public function getUseDistinctCount()
215
    {
216
        return $this->useDistinctCount;
217
    }
218
}
219