Completed
Push — release-1.x ( f5fa1d )
by Boy
11s
created

RaSecondFactorSearchQuery   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 221
Duplicated Lines 13.12 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 1
cbo 1
dl 29
loc 221
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
A getName() 0 4 1
A setName() 0 6 1
A getType() 0 4 1
A setType() 0 6 1
A getSecondFactorId() 0 4 1
A setSecondFactorId() 0 6 1
A getEmail() 0 4 1
A setEmail() 0 6 1
A getStatus() 0 4 1
A setStatus() 0 9 2
A setOrderBy() 0 6 1
A setOrderDirection() 9 9 2
A assertNonEmptyString() 10 10 2
A toHttpQuery() 0 21 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 RaSecondFactorSearchQuery implements HttpQuery
25
{
26
    const STATUS_UNVERIFIED = 'unverified';
27
    const STATUS_VERIFIED = 'verified';
28
    const STATUS_VETTED = 'vetted';
29
    const STATUS_REVOKED = 'revoked';
30
31
    /**
32
     * @var string
33
     */
34
    private $institution;
35
36
    /**
37
     * @var string|null
38
     */
39
    private $name;
40
41
    /**
42
     * @var string|null
43
     */
44
    private $type;
45
46
    /**
47
     * @var string|null The second factor type's ID (eg. Yubikey public ID)
48
     */
49
    private $secondFactorId;
50
51
    /**
52
     * @var string|null
53
     */
54
    private $email;
55
56
    /**
57
     * @var string|null One of the STATUS_* constants.
58
     */
59
    private $status;
60
61
    /**
62
     * @var string|null
63
     */
64
    private $orderBy;
65
66
    /**
67
     * @var string|null
68
     */
69
    private $orderDirection;
70
71
    /**
72
     * @var int
73
     */
74
    private $pageNumber;
75
76
    /**
77
     * @param string $institution
78
     * @param int $pageNumber
79
     */
80 View Code Duplication
    public function __construct($institution, $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($institution, 'institution');
83
        Assert\that($pageNumber)
84
            ->integer('Page number must be an integer')
85
            ->min(1, 'Page number must be greater than or equal to 1');
86
87
        $this->institution = $institution;
88
        $this->pageNumber = $pageNumber;
89
    }
90
91
    /**
92
     * @return null|string
93
     */
94
    public function getName()
95
    {
96
        return $this->name;
97
    }
98
99
    /**
100
     * @param null|string $name
101
     */
102
    public function setName($name)
103
    {
104
        $this->assertNonEmptyString($name, 'name');
105
106
        $this->name = $name;
107
    }
108
109
    /**
110
     * @return null|string
111
     */
112
    public function getType()
113
    {
114
        return $this->type;
115
    }
116
117
    /**
118
     * @param null|string $type
119
     */
120
    public function setType($type)
121
    {
122
        $this->assertNonEmptyString($type, 'type');
123
124
        $this->type = $type;
125
    }
126
127
    /**
128
     * @return null|string
129
     */
130
    public function getSecondFactorId()
131
    {
132
        return $this->secondFactorId;
133
    }
134
135
    /**
136
     * @param null|string $secondFactorId
137
     */
138
    public function setSecondFactorId($secondFactorId)
139
    {
140
        $this->assertNonEmptyString($secondFactorId, 'secondFactorId');
141
142
        $this->secondFactorId = $secondFactorId;
143
    }
144
145
    /**
146
     * @return null|string
147
     */
148
    public function getEmail()
149
    {
150
        return $this->email;
151
    }
152
153
    /**
154
     * @param null|string $email
155
     */
156
    public function setEmail($email)
157
    {
158
        $this->assertNonEmptyString($email, 'email');
159
160
        $this->email = $email;
161
    }
162
163
    /**
164
     * @return null|string
165
     */
166
    public function getStatus()
167
    {
168
        return $this->status;
169
    }
170
171
    /**
172
     * @param string $status
173
     */
174
    public function setStatus($status)
175
    {
176
        Assert\that($status)->choice(
177
            [self::STATUS_UNVERIFIED, self::STATUS_VERIFIED, self::STATUS_VETTED, self::STATUS_REVOKED, ''],
178
            'Invalid second factor status, must be one of the STATUS constants'
179
        );
180
181
        $this->status = $status ?: null;
182
    }
183
184
    /**
185
     * @param string $orderBy
186
     */
187
    public function setOrderBy($orderBy)
188
    {
189
        $this->assertNonEmptyString($orderBy, 'orderBy');
190
191
        $this->orderBy = $orderBy;
192
    }
193
194
    /**
195
     * @param string|null $orderDirection
196
     */
197 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...
198
    {
199
        Assert\that($orderDirection)->choice(
200
            ['asc', 'desc', '', null],
201
            "Invalid order direction, must be one of 'asc', 'desc'"
202
        );
203
204
        $this->orderDirection = $orderDirection ?: null;
205
    }
206
207 View Code Duplication
    private function assertNonEmptyString($value, $name)
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...
208
    {
209
        $message = sprintf(
210
            '"%s" must be a non-empty string, "%s" given',
211
            $name,
212
            (is_object($value) ? get_class($value) : gettype($value))
213
        );
214
215
        Assert\that($value)->string($message)->notEmpty($message);
216
    }
217
218
    /**
219
     * Return the Http Query string as should be used, MUST include the '?' prefix.
220
     *
221
     * @return string
222
     */
223
    public function toHttpQuery()
224
    {
225
        return '?' . http_build_query(
226
            array_filter(
227
                [
228
                    'institution'    => $this->institution,
229
                    'name'           => $this->name,
230
                    'type'           => $this->type,
231
                    'secondFactorId' => $this->secondFactorId,
232
                    'email'          => $this->email,
233
                    'status'         => $this->status,
234
                    'orderBy'        => $this->orderBy,
235
                    'orderDirection' => $this->orderDirection,
236
                    'p'              => $this->pageNumber,
237
                ],
238
                function ($value) {
239
                    return !is_null($value);
240
                }
241
            )
242
        );
243
    }
244
}
245