RaListingSearchQuery   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 200
Duplicated Lines 21.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 43
loc 200
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setInstitution() 0 8 1
A setIdentityId() 0 8 1
A setOrderBy() 0 8 1
A setOrderDirection() 11 11 2
A setName() 0 6 1
A setEmail() 0 6 1
A setRole() 0 6 1
A setRaInstitution() 0 6 1
A assertNonEmptyString() 0 10 2
A toHttpQuery() 22 22 1
A __construct() 10 10 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
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupMiddlewareClient\Identity\Dto;
20
21
use Assert;
22
use Surfnet\StepupMiddlewareClient\Dto\HttpQuery;
23
24
final class RaListingSearchQuery implements HttpQuery
25
{
26
    /**
27
     * @var string
28
     */
29
    private $actorId;
30
31
    /**
32
     * @var string
33
     */
34
    private $name;
35
36
    /**
37
     * @var string
38
     */
39
    private $email;
40
41
    /**
42
     * @var string
43
     */
44
    private $role;
45
46
    /**
47
     * @var string
48
     */
49
    private $raInstitution;
50
51
    /**
52
     * @var string|null
53
     */
54
    private $institution;
55
56
    /**
57
     * @var string|null
58
     */
59
    private $identityId;
60
61
    /**
62
     * @var int
63
     */
64
    private $pageNumber;
65
66
    /**
67
     * @var string|null
68
     */
69
    private $orderBy = 'commonName';
70
71
    /**
72
     * @var string|null
73
     */
74
    private $orderDirection = 'asc';
75
76
    /**
77
     * @param string $actorId
78
     * @param int $pageNumber
79
     */
80 View Code Duplication
    public function __construct($actorId, $pageNumber)
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...
81
    {
82
        $this->assertNonEmptyString($actorId, 'actorId');
83
        Assert\that($pageNumber)
84
            ->integer('Page number must be an integer')
85
            ->min(0, 'Page number must be greater than or equal to 1');
86
87
        $this->actorId = $actorId;
88
        $this->pageNumber  = $pageNumber;
89
    }
90
91
    /**
92
     * @param string $institution
93
     * @return $this
94
     */
95
    public function setInstitution($institution)
96
    {
97
        $this->assertNonEmptyString($institution, 'institution');
98
99
        $this->institution = $institution;
100
101
        return $this;
102
    }
103
104
    /**
105
     * @param string $identityId
106
     * @return RaListingSearchQuery
107
     */
108
    public function setIdentityId($identityId)
109
    {
110
        $this->assertNonEmptyString($identityId, 'identityId');
111
112
        $this->identityId = $identityId;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @param string $orderBy
119
     * @return RaListingSearchQuery
120
     */
121
    public function setOrderBy($orderBy)
122
    {
123
        $this->assertNonEmptyString($orderBy, 'orderBy');
124
125
        $this->orderBy = $orderBy;
126
127
        return $this;
128
    }
129
130
    /**
131
     * @param string|null $orderDirection
132
     * @return RaListingSearchQuery
133
     */
134 View Code Duplication
    public function setOrderDirection($orderDirection)
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...
135
    {
136
        Assert\that($orderDirection)->choice(
137
            ['asc', 'desc', '', null],
138
            "Invalid order direction, must be one of 'asc', 'desc'"
139
        );
140
141
        $this->orderDirection = $orderDirection ?: null;
142
143
        return $this;
144
    }
145
146
    /**
147
     * @param string $name
148
     * @return RaListingSearchQuery
149
     */
150
    public function setName($name)
151
    {
152
        $this->assertNonEmptyString($name, 'name');
153
        $this->name = $name;
154
        return $this;
155
    }
156
157
    /**
158
     * @param string $email
159
     * @return RaListingSearchQuery
160
     */
161
    public function setEmail($email)
162
    {
163
        $this->assertNonEmptyString($email, 'email');
164
        $this->email = $email;
165
        return $this;
166
    }
167
168
    /**
169
     * @param string $role
170
     * @return RaListingSearchQuery
171
     */
172
    public function setRole($role)
173
    {
174
        $this->assertNonEmptyString($role, 'role');
175
        $this->role = $role;
176
        return $this;
177
    }
178
179
    /**
180
     * @param string $raInstitution
181
     * @return RaListingSearchQuery
182
     */
183
    public function setRaInstitution($raInstitution)
184
    {
185
        $this->assertNonEmptyString($raInstitution, 'raInstitution');
186
        $this->raInstitution = $raInstitution;
187
        return $this;
188
    }
189
190
    private function assertNonEmptyString($value, $parameterName)
191
    {
192
        $message = sprintf(
193
            '"%s" must be a non-empty string, "%s" given',
194
            $parameterName,
195
            (is_object($value) ? get_class($value) : gettype($value))
196
        );
197
198
        Assert\that($value)->string($message)->notEmpty($message);
199
    }
200
201 View Code Duplication
    public function toHttpQuery()
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...
202
    {
203
        return '?'.http_build_query(
204
            array_filter(
205
                [
206
                    'actorId' => $this->actorId,
207
                    'institution' => $this->institution,
208
                    'identityId' => $this->identityId,
209
                    'name' => $this->name,
210
                    'email' => $this->email,
211
                    'role' => $this->role,
212
                    'raInstitution' => $this->raInstitution,
213
                    'orderBy' => $this->orderBy,
214
                    'orderDirection' => $this->orderDirection,
215
                    'p' => $this->pageNumber,
216
                ],
217
                function ($value) {
218
                    return !is_null($value);
219
                }
220
            )
221
        );
222
    }
223
}
224