RaSecondFactorExportQuery   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 263
Duplicated Lines 14.83 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 1
dl 39
loc 263
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setActorId() 0 8 1
A getFileName() 0 17 3
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 getInstitution() 0 4 1
A setInstitution() 0 4 1
A getStatus() 0 4 1
A setStatus() 9 9 2
A setOrderBy() 0 6 1
A setOrderDirection() 9 9 2
A assertNonEmptyString() 0 10 2
A toHttpQuery() 21 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 DateTime;
23
use Surfnet\StepupMiddlewareClient\Dto\HttpQuery;
24
25
final class RaSecondFactorExportQuery implements HttpQuery
26
{
27
    const STATUS_UNVERIFIED = 'unverified';
28
    const STATUS_VERIFIED = 'verified';
29
    const STATUS_VETTED = 'vetted';
30
    const STATUS_REVOKED = 'revoked';
31
32
    /**
33
     * @var string|null
34
     */
35
    private $name;
36
37
    /**
38
     * @var string|null
39
     */
40
    private $type;
41
42
    /**
43
     * @var string|null The second factor type's ID (eg. Yubikey public ID)
44
     */
45
    private $secondFactorId;
46
47
    /**
48
     * @var string|null
49
     */
50
    private $email;
51
52
    /**
53
     * @var string|null
54
     */
55
    private $institution;
56
57
    /**
58
     * @var string|null One of the STATUS_* constants.
59
     */
60
    private $status;
61
62
    /**
63
     * @var string|null
64
     */
65
    private $orderBy;
66
67
    /**
68
     * @var string|null
69
     */
70
    private $orderDirection;
71
72
    /**
73
     * @var string
74
     */
75
    private $actorId;
76
77
    /**
78
     * @param string $actorId
79
     */
80
    public function __construct($actorId)
81
    {
82
        $this->assertNonEmptyString($actorId, 'actorId');
83
84
        $this->actorId = $actorId;
85
    }
86
87
    /**
88
     * @param string $actorInstitution
0 ignored issues
show
Bug introduced by
There is no parameter named $actorInstitution. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
89
     * @return VerifiedSecondFactorSearchQuery
0 ignored issues
show
Documentation introduced by
Should the return type not be RaSecondFactorExportQuery?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
90
     */
91
    public function setActorId($actorId)
92
    {
93
        $this->assertNonEmptyString($actorId, 'actorId');
94
95
        $this->actorId = $actorId;
96
97
        return $this;
98
    }
99
100
    public function getFileName()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
101
    {
102
        $date = new DateTime();
103
        $date = $date->format('Y-m-d');
104
105
        $fileName = "token_export_{$date}";
106
107
        if ($this->type) {
108
            $fileName .= "-{$this->type}";
109
        }
110
111
        if ($this->status) {
112
            $fileName .= "-{$this->status}";
113
        }
114
115
        return $fileName;
116
    }
117
118
    /**
119
     * @return null|string
120
     */
121
    public function getName()
122
    {
123
        return $this->name;
124
    }
125
126
    /**
127
     * @param null|string $name
128
     */
129
    public function setName($name)
130
    {
131
        $this->assertNonEmptyString($name, 'name');
132
133
        $this->name = $name;
134
    }
135
136
    /**
137
     * @return null|string
138
     */
139
    public function getType()
140
    {
141
        return $this->type;
142
    }
143
144
    /**
145
     * @param null|string $type
146
     */
147
    public function setType($type)
148
    {
149
        $this->assertNonEmptyString($type, 'type');
150
151
        $this->type = $type;
152
    }
153
154
    /**
155
     * @return null|string
156
     */
157
    public function getSecondFactorId()
158
    {
159
        return $this->secondFactorId;
160
    }
161
162
    /**
163
     * @param null|string $secondFactorId
164
     */
165
    public function setSecondFactorId($secondFactorId)
166
    {
167
        $this->assertNonEmptyString($secondFactorId, 'secondFactorId');
168
169
        $this->secondFactorId = $secondFactorId;
170
    }
171
172
    /**
173
     * @return null|string
174
     */
175
    public function getEmail()
176
    {
177
        return $this->email;
178
    }
179
180
    /**
181
     * @param null|string $email
182
     */
183
    public function setEmail($email)
184
    {
185
        $this->assertNonEmptyString($email, 'email');
186
187
        $this->email = $email;
188
    }
189
190
    /**
191
     * @return null|string
192
     */
193
    public function getInstitution()
194
    {
195
        return $this->institution;
196
    }
197
198
    /**
199
     * @param null|string $institution
200
     */
201
    public function setInstitution($institution)
202
    {
203
        $this->institution = $institution;
204
    }
205
206
    /**
207
     * @return null|string
208
     */
209
    public function getStatus()
210
    {
211
        return $this->status;
212
    }
213
214
    /**
215
     * @param string $status
216
     */
217 View Code Duplication
    public function setStatus($status)
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...
218
    {
219
        Assert\that($status)->choice(
220
            [self::STATUS_UNVERIFIED, self::STATUS_VERIFIED, self::STATUS_VETTED, self::STATUS_REVOKED, ''],
221
            'Invalid second factor status, must be one of the STATUS constants'
222
        );
223
224
        $this->status = $status ?: null;
225
    }
226
227
    /**
228
     * @param string $orderBy
229
     */
230
    public function setOrderBy($orderBy)
231
    {
232
        $this->assertNonEmptyString($orderBy, 'orderBy');
233
234
        $this->orderBy = $orderBy;
235
    }
236
237
    /**
238
     * @param string|null $orderDirection
239
     */
240 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...
241
    {
242
        Assert\that($orderDirection)->choice(
243
            ['asc', 'desc', '', null],
244
            "Invalid order direction, must be one of 'asc', 'desc'"
245
        );
246
247
        $this->orderDirection = $orderDirection ?: null;
248
    }
249
250
    private function assertNonEmptyString($value, $name)
251
    {
252
        $message = sprintf(
253
            '"%s" must be a non-empty string, "%s" given',
254
            $name,
255
            (is_object($value) ? get_class($value) : gettype($value))
256
        );
257
258
        Assert\that($value)->string($message)->notEmpty($message);
259
    }
260
261
    /**
262
     * Return the Http Query string as should be used, MUST include the '?' prefix.
263
     *
264
     * @return string
265
     */
266 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...
267
    {
268
        return '?' . http_build_query(
269
            array_filter(
270
                [
271
                    'actorId'          => $this->actorId,
272
                    'name'             => $this->name,
273
                    'type'             => $this->type,
274
                    'secondFactorId'   => $this->secondFactorId,
275
                    'email'            => $this->email,
276
                    'institution'      => $this->institution,
277
                    'status'           => $this->status,
278
                    'orderBy'          => $this->orderBy,
279
                    'orderDirection'   => $this->orderDirection
280
                ],
281
                function ($value) {
282
                    return !is_null($value);
283
                }
284
            )
285
        );
286
    }
287
}
288