Completed
Push — develop ( fe8037...d3bd6d )
by
unknown
05:36 queued 04:03
created

RaCandidateSearchQuery::setInstitution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
class RaCandidateSearchQuery implements HttpQuery
25
{
26
    /**
27
     * @var string
28
     */
29
    private $actorId;
30
31
    /**
32
     * @var string
33
     */
34
    private $institution;
35
36
    /**
37
     * @var string
38
     */
39
    private $commonName;
40
41
    /**
42
     * @var string
43
     */
44
    private $email;
45
46
    /**
47
     * @var string
48
     */
49
    private $raInstitution;
50
51
    /**
52
     * @var int
53
     */
54
    private $pageNumber = 1;
55
56
    /**
57
     * @var string
58
     */
59
    private $orderBy;
60
61
    /**
62
     * @var string
63
     */
64
    private $orderDirection;
65
66
    /**
67
     * @var string[]
68
     */
69
    private $secondFactorTypes = [];
70
71
    /**
72
     * @param string $actorId
73
     * @param int $pageNumber
74
     */
75 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...
76
    {
77
        $this->assertNonEmptyString($actorId, 'actorId');
78
        Assert\that($pageNumber)
79
            ->integer('Page number must be an integer')
80
            ->min(0, 'Page number must be greater than or equal to 1');
81
82
        $this->actorId = $actorId;
83
        $this->pageNumber  = $pageNumber;
84
    }
85
86
    /**
87
     * @param string $commonName
88
     * @return $this
89
     */
90
    public function setCommonName($commonName)
91
    {
92
        $this->assertNonEmptyString($commonName, 'commonName');
93
94
        $this->commonName = $commonName;
95
96
        return $this;
97
    }
98
99
    /**
100
     * @param string $email
101
     * @return $this
102
     */
103
    public function setEmail($email)
104
    {
105
        $this->assertNonEmptyString($email, 'email');
106
107
        $this->email = $email;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @param string $institution
114
     * @return $this
115
     */
116
    public function setInstitution($institution)
117
    {
118
        $this->assertNonEmptyString($institution, 'institution');
119
120
        $this->institution = $institution;
121
122
        return $this;
123
    }
124
125
    /**
126
     * @param string $raInstitution
127
     */
128
    public function setRaInstitution($raInstitution)
129
    {
130
        $this->raInstitution = $raInstitution;
131
    }
132
133
    /**
134
     * @param string $role
135
     * @return $this
136
     */
137
    public function setRole($role)
138
    {
139
        $this->assertNonEmptyString($role, 'role');
140
141
        $this->role = $role;
0 ignored issues
show
Bug introduced by
The property role does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
142
143
        return $this;
144
    }
145
146
    /**
147
     * @param array $secondFactorTypes
148
     *
149
     * @return void
150
     */
151
    public function setSecondFactorTypes(array $secondFactorTypes)
152
    {
153
        $this->assertAllNonEmptyString($secondFactorTypes, 'secondFactorTypes');
154
155
        $this->secondFactorTypes = $secondFactorTypes;
156
    }
157
158
    /**
159
     * @param string $orderBy
160
     * @return $this
161
     */
162
    public function setOrderBy($orderBy)
163
    {
164
        $this->assertNonEmptyString($orderBy, 'orderBy');
165
166
        $this->orderBy = $orderBy;
167
168
        return $this;
169
    }
170
171
    /**
172
     * @param string $orderDirection
173
     * @return $this
174
     */
175
    public function setOrderDirection($orderDirection)
176
    {
177
        $this->assertNonEmptyString($orderDirection, 'orderDirection');
178
        Assert\that($orderDirection)->choice(
179
            ['asc', 'desc', '', null],
180
            "Invalid order direction, must be one of 'asc', 'desc'"
181
        );
182
183
        $this->orderDirection = $orderDirection;
184
185
        return $this;
186
    }
187
188 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...
189
    {
190
        return '?' . http_build_query(
191
            array_filter(
192
                [
193
                    'actorId'           => $this->actorId,
194
                    'institution'       => $this->institution,
195
                    'commonName'        => $this->commonName,
196
                    'email'             => $this->email,
197
                    'raInstitution'     => $this->raInstitution,
198
                    'secondFactorTypes' => $this->secondFactorTypes,
199
                    'orderBy'           => $this->orderBy,
200
                    'orderDirection'    => $this->orderDirection,
201
                    'p'                 => $this->pageNumber,
202
                ],
203
                function ($value) {
204
                    return !is_null($value);
205
                }
206
            )
207
        );
208
    }
209
210
    /**
211
     * @param mixed       $value
212
     * @param string      $parameterName
213
     * @param string|null $message
214
     */
215
    private function assertNonEmptyString($value, $parameterName, $message = null)
216
    {
217
        $message = sprintf(
218
            $message ?: '"%s" must be a non-empty string, "%s" given',
219
            $parameterName,
220
            (is_object($value) ? get_class($value) : gettype($value))
221
        );
222
223
        Assert\that($value)->string($message)->notEmpty($message);
224
    }
225
226
    /**
227
     * @param array $values
228
     * @param string $parameterName
229
     *
230
     * @return void
231
     */
232
    private function assertAllNonEmptyString(array $values, $parameterName)
233
    {
234
        foreach ($values as $value) {
235
            $this->assertNonEmptyString(
236
                $value,
237
                $parameterName,
238
                'Elements of "%s" must be non-empty strings, element of type "%s" given'
239
            );
240
        }
241
    }
242
}
243