DocumentDataSource::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB;
13
14
use Cubiche\Core\Comparable\ComparatorInterface;
15
use Cubiche\Core\Specification\SpecificationInterface;
16
use Cubiche\Core\Collections\DataSource\DataSource;
17
use Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\Query\QueryBuilder;
18
use Doctrine\ODM\MongoDB\DocumentManager;
19
use Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\Query\SpecificationVisitorFactoryInterface;
20
use Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\Query\ComparatorVisitorFactoryInterface;
21
22
/**
23
 * Document Data Source Class.
24
 *
25
 * @author Karel Osorio Ramírez <[email protected]>
26
 */
27
class DocumentDataSource extends DataSource
28
{
29
    /**
30
     * @var string
31
     */
32
    protected $documentName;
33
34
    /**
35
     * @var DocumentManager
36
     */
37
    protected $dm;
38
39
    /**
40
     * @var SpecificationVisitorFactoryInterface
41
     */
42
    protected $specificationVisitorFactory;
43
44
    /**
45
     * @var ComparatorVisitorFactoryInterface
46
     */
47
    protected $comparatorVisitorFactory;
48
49
    /**
50
     * @var QueryBuilder
51
     */
52
    private $queryBuilder;
53
54
    /**
55
     * @param DocumentManager                      $dm
56
     * @param SpecificationVisitorFactoryInterface $specificationVisitorFactory
57
     * @param ComparatorVisitorFactoryInterface    $comparatorVisitorFactory
58
     * @param string                               $documentName
59
     * @param SpecificationInterface               $searchCriteria
60
     * @param ComparatorInterface                  $sortCriteria
61
     * @param string                               $offset
62
     * @param string                               $length
63
     */
64
    public function __construct(
65
        DocumentManager $dm,
66
        SpecificationVisitorFactoryInterface $specificationVisitorFactory,
67
        ComparatorVisitorFactoryInterface $comparatorVisitorFactory,
68
        $documentName = null,
69
        SpecificationInterface $searchCriteria = null,
70
        ComparatorInterface $sortCriteria = null,
71
        $offset = null,
72
        $length = null
73
    ) {
74
        parent::__construct($searchCriteria, $sortCriteria, $offset, $length);
75
76
        $this->dm = $dm;
77
        $this->specificationVisitorFactory = $specificationVisitorFactory;
78
        $this->comparatorVisitorFactory = $comparatorVisitorFactory;
79
        $this->documentName = $documentName;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getIterator()
86
    {
87
        return $this->queryBuilder()->getQuery()->execute();
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function findOne()
94
    {
95
        return $this->queryBuilder()->getQuery()->getSingleResult();
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function filteredDataSource(SpecificationInterface $criteria)
102
    {
103
        if ($this->isFiltered()) {
104
            $criteria = $this->searchCriteria()->andX($criteria);
105
        }
106
107
        return $this->createDocumentDataSource(
108
            $criteria,
109
            $this->sortCriteria(),
110
            $this->offset(),
111
            $this->length()
112
        );
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function slicedDataSource($offset, $length = null)
119
    {
120
        return $this->createDocumentDataSource(
121
            $this->searchCriteria(),
122
            $this->sortCriteria(),
123
            $this->actualOffset($offset),
124
            $this->actualLength($offset, $length)
125
        );
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function sortedDataSource(ComparatorInterface $sortCriteria)
132
    {
133
        return $this->createDocumentDataSource(
134
            $this->searchCriteria(),
135
            $sortCriteria,
136
            $this->offset(),
137
            $this->length()
138
        );
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    protected function calculateCount()
145
    {
146
        return $this->queryBuilder()->getQuery()->count(true);
147
    }
148
149
    /**
150
     * @return \Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\QueryBuilder
151
     */
152
    protected function queryBuilder()
153
    {
154
        if ($this->queryBuilder === null) {
155
            $this->queryBuilder = new QueryBuilder(
156
                $this->dm,
157
                $this->documentName,
158
                $this->specificationVisitorFactory,
159
                $this->comparatorVisitorFactory
160
            );
161
            if ($this->isFiltered()) {
162
                $this->queryBuilder->addSearchCriteria($this->searchCriteria());
163
            }
164
            if ($this->offset !== null) {
165
                $this->queryBuilder->skip($this->offset);
166
            }
167
            if ($this->length !== null) {
168
                $this->queryBuilder->limit($this->length);
169
            }
170
            if ($this->isSorted()) {
171
                $this->queryBuilder->addSortCriteria($this->sortCriteria());
172
            }
173
        }
174
175
        return $this->queryBuilder;
176
    }
177
178
    /**
179
     * @param SpecificationInterface $searchCriteria
180
     * @param ComparatorInterface    $sortCriteria
181
     * @param string                 $offset
182
     * @param string                 $length
183
     *
184
     * @return DocumentDataSource
185
     */
186
    protected function createDocumentDataSource(
187
        SpecificationInterface $searchCriteria = null,
188
        ComparatorInterface $sortCriteria = null,
189
        $offset = null,
190
        $length = null
191
    ) {
192
        return new self(
193
            $this->dm,
194
            $this->specificationVisitorFactory,
195
            $this->comparatorVisitorFactory,
196
            $this->documentName,
197
            $searchCriteria,
198
            $sortCriteria,
199
            $offset,
200
            $length
201
        );
202
    }
203
}
204