Completed
Push — master ( 74c3c7...591b88 )
by Eric
08:09
created

DataSourceBuilder::getAliases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Grid\DataSource\Doctrine\MongoDB;
13
14
use Doctrine\ODM\MongoDB\Query\Builder;
15
use Lug\Component\Grid\DataSource\ArrayDataSource;
16
use Lug\Component\Grid\DataSource\DataSourceBuilderInterface;
17
use Lug\Component\Grid\DataSource\PagerfantaDataSource;
18
use Lug\Component\Resource\Repository\Doctrine\MongoDB\Repository;
19
use Pagerfanta\Adapter\DoctrineODMMongoDBAdapter;
20
21
/**
22
 * @author GeLo <[email protected]>
23
 */
24
class DataSourceBuilder implements DataSourceBuilderInterface
25
{
26
    /**
27
     * @var Repository
28
     */
29
    private $repository;
30
31
    /**
32
     * @var Builder
33
     */
34
    private $queryBuilder;
35
36
    /**
37
     * @var ExpressionBuilder
38
     */
39
    private $expressionBuilder;
40
41
    /**
42
     * @var int
43
     */
44
    private $limit = 10;
45
46
    /**
47
     * @var int
48
     */
49
    private $page = 1;
50
51
    /**
52
     * @param Repository $repository
53
     * @param mixed[]    $options
54
     */
55 18 View Code Duplication
    public function __construct(Repository $repository, array $options = [])
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...
56
    {
57 18
        $repositoryMethod = isset($options['repository_method'])
58 18
            ? $options['repository_method']
59 18
            : 'createQueryBuilderForCollection';
60
61 18
        $this->repository = $repository;
62 18
        $this->queryBuilder = $this->repository->$repositoryMethod();
63 18
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    public function select($select)
69
    {
70 1
        return $this;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 1
    public function innerJoin($join, $alias)
77
    {
78 1
        $this->queryBuilder->field($join)->notEqual(null);
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 1
    public function leftJoin($join, $alias)
87
    {
88 1
        return $this;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 1
    public function andWhere($where)
95
    {
96 1
        $this->queryBuilder->addAnd($where);
0 ignored issues
show
Documentation introduced by
$where is of type string, but the function expects a array|object<Doctrine\MongoDB\Query\Expr>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
97
98 1
        return $this;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 1
    public function orWhere($where)
105
    {
106 1
        $this->queryBuilder->addOr($where);
0 ignored issues
show
Documentation introduced by
$where is of type string, but the function expects a array|object<Doctrine\MongoDB\Query\Expr>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
108 1
        return $this;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 2
    public function orderBy($sort, $order = 'ASC')
115
    {
116 2
        $this->queryBuilder->sort($sort, $order);
117
118 2
        return $this;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 1
    public function setLimit($limit)
125
    {
126 1
        $this->limit = $limit;
127
128 1
        return $this;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 1
    public function setPage($page)
135
    {
136 1
        $this->page = $page;
137
138 1
        return $this;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 1
    public function setParameter($parameter, $value, $type = null)
145
    {
146 1
        return $this;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152 1
    public function createPlaceholder($parameter, $value, $type = null)
153
    {
154 1
        return $value;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 1
    public function getProperty($field)
161
    {
162 1
        return $this->repository->getProperty($field, $this->queryBuilder);
0 ignored issues
show
Documentation introduced by
$this->queryBuilder is of type object<Doctrine\ODM\MongoDB\Query\Builder>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168 1
    public function getAliases()
169
    {
170 1
        return [];
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176 1
    public function getExpressionBuilder()
177
    {
178 1
        if ($this->expressionBuilder === null) {
179 1
            $this->expressionBuilder = new ExpressionBuilder(clone $this->queryBuilder);
180 1
        }
181
182 1
        return $this->expressionBuilder;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188 4 View Code Duplication
    public function createDataSource(array $options = [])
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...
189
    {
190 4
        $queryBuilder = clone $this->queryBuilder;
191
192 4
        if (isset($options['all']) && $options['all']) {
193 1
            return new ArrayDataSource($queryBuilder->getQuery()->getIterator()->toArray());
194
        }
195
196 3
        $dataSource = new PagerfantaDataSource(new DoctrineODMMongoDBAdapter($queryBuilder));
197 3
        $dataSource->setMaxPerPage($this->limit);
198 3
        $dataSource->setCurrentPage($this->page);
199
200 3
        return $dataSource;
201
    }
202
}
203