Passed
Pull Request — master (#10)
by Gocha
11:52
created

ConfiguredQuery   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 136
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 2
dl 136
loc 136
ccs 0
cts 68
cp 0
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A addOrderingConfiguration() 10 10 3
A addOrderingConfigurations() 8 8 2
A getOrderingConfigurationFor() 8 8 2
A getQueryBuilder() 4 4 1
A isTotalCountNeeded() 4 4 1
A setTotalCountNeeded() 5 5 1
A setMaximumOffset() 5 5 1
A hasMaximumOffset() 4 4 1
A getMaximumOffset() 8 8 2
A getItemTransformer() 4 4 1
A setItemTransformer() 6 6 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
declare(strict_types=1);
3
4
namespace Paysera\Pagination\Entity\ODM;
5
6
use Doctrine\ODM\MongoDB\Query\Builder as QueryBuilder;
7
use Paysera\Pagination\Entity\OrderingConfiguration;
8
use Paysera\Pagination\Exception\InvalidOrderByException;
9
use InvalidArgumentException;
10
use RuntimeException;
11
12 View Code Duplication
class ConfiguredQuery
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    /**
15
     * @var QueryBuilder
16
     */
17
    private $queryBuilder;
18
19
    /**
20
     * @var array|OrderingConfiguration[] associative, keys are strings for ordering fields
21
     */
22
    private $orderingConfigurations;
23
24
    /**
25
     * @var bool
26
     */
27
    private $totalCountNeeded;
28
29
    /**
30
     * @var int|null
31
     */
32
    private $maximumOffset;
33
34
    /**
35
     * @var callable
36
     */
37
    private $itemTransformer;
38
39
    public function __construct(QueryBuilder $queryBuilder)
40
    {
41
        $this->queryBuilder = $queryBuilder;
42
        $this->orderingConfigurations = [];
43
        $this->totalCountNeeded = false;
44
    }
45
46
    public function addOrderingConfiguration(string $orderBy, OrderingConfiguration $configuration): self
47
    {
48
        if ($configuration->getAccessorPath() === null && $configuration->getAccessorClosure() === null) {
49
            throw new InvalidArgumentException(
50
                'Must set either accessorPath or accessorClosure for every OrderingConfiguration'
51
            );
52
        }
53
        $this->orderingConfigurations[$orderBy] = $configuration;
54
        return $this;
55
    }
56
57
    /**
58
     * @param array|OrderingConfiguration[] $orderingConfigurations array of `orderBy => OrderingConfiguration` pairs
59
     * @return ConfiguredQuery
60
     */
61
    public function addOrderingConfigurations(array $orderingConfigurations): self
62
    {
63
        foreach ($orderingConfigurations as $orderBy => $configuration) {
64
            $this->addOrderingConfiguration($orderBy, $configuration);
65
        }
66
67
        return $this;
68
    }
69
70
    public function getOrderingConfigurationFor(string $orderBy): OrderingConfiguration
71
    {
72
        if (!isset($this->orderingConfigurations[$orderBy])) {
73
            throw new InvalidOrderByException($orderBy);
74
        }
75
76
        return $this->orderingConfigurations[$orderBy];
77
    }
78
79
    /**
80
     * @return QueryBuilder
81
     */
82
    public function getQueryBuilder(): QueryBuilder
83
    {
84
        return $this->queryBuilder;
85
    }
86
87
    /**
88
     * @return bool
89
     */
90
    public function isTotalCountNeeded(): bool
91
    {
92
        return $this->totalCountNeeded;
93
    }
94
95
    /**
96
     * @param bool $totalCountNeeded
97
     * @return $this
98
     */
99
    public function setTotalCountNeeded(bool $totalCountNeeded): self
100
    {
101
        $this->totalCountNeeded = $totalCountNeeded;
102
        return $this;
103
    }
104
105
    /**
106
     * @param int $maximumOffset
107
     * @return $this
108
     */
109
    public function setMaximumOffset(int $maximumOffset): self
110
    {
111
        $this->maximumOffset = $maximumOffset;
112
        return $this;
113
    }
114
115
    public function hasMaximumOffset(): bool
116
    {
117
        return $this->maximumOffset !== null;
118
    }
119
120
    /**
121
     * @return int
122
     * @throws RuntimeException if maximum offset was not set. Check with hasMaximumOffset beforehand
123
     */
124
    public function getMaximumOffset(): int
125
    {
126
        if ($this->maximumOffset === null) {
127
            throw new RuntimeException('Maximum offset was not set');
128
        }
129
130
        return $this->maximumOffset;
131
    }
132
133
    /**
134
     * @return callable|null
135
     */
136
    public function getItemTransformer()
137
    {
138
        return $this->itemTransformer;
139
    }
140
141
    public function setItemTransformer(callable $itemTransformer): self
142
    {
143
        $this->itemTransformer = $itemTransformer;
144
145
        return $this;
146
    }
147
}
148