Completed
Push — master ( d2aa7f...78f4d4 )
by Michiel
03:30 queued 02:21
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 $actorInstitution;
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 int
48
     */
49
    private $pageNumber = 1;
50
51
    /**
52
     * @var string
53
     */
54
    private $orderBy;
55
56
    /**
57
     * @var string
58
     */
59
    private $orderDirection;
60
61
    /**
62
     * @var string[]
63
     */
64
    private $secondFactorTypes = [];
65
66
    /**
67
     * @param string $institution
0 ignored issues
show
Documentation introduced by
There is no parameter named $institution. Did you maybe mean $actorInstitution?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
68
     * @param int    $pageNumber
69
     */
70 View Code Duplication
    public function __construct($actorInstitution, $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...
71
    {
72
        $this->assertNonEmptyString($actorInstitution, 'actorInstitution');
73
        Assert\that($pageNumber)
74
            ->integer('Page number must be an integer')
75
            ->min(0, 'Page number must be greater than or equal to 1');
76
77
        $this->actorInstitution = $actorInstitution;
78
        $this->pageNumber  = $pageNumber;
79
    }
80
81
    /**
82
     * @param string $commonName
83
     * @return $this
84
     */
85
    public function setCommonName($commonName)
86
    {
87
        $this->assertNonEmptyString($commonName, 'commonName');
88
89
        $this->commonName = $commonName;
90
91
        return $this;
92
    }
93
94
    /**
95
     * @param string $email
96
     * @return $this
97
     */
98
    public function setEmail($email)
99
    {
100
        $this->assertNonEmptyString($email, 'email');
101
102
        $this->email = $email;
103
104
        return $this;
105
    }
106
107
    /**
108
     * @param string $institution
109
     * @return $this
110
     */
111
    public function setInstitution($institution)
112
    {
113
        $this->assertNonEmptyString($institution, 'institution');
114
115
        $this->institution = $institution;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param string $role
122
     * @return $this
123
     */
124
    public function setRole($role)
125
    {
126
        $this->assertNonEmptyString($role, 'role');
127
128
        $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...
129
130
        return $this;
131
    }
132
133
    /**
134
     * @param array $secondFactorTypes
135
     *
136
     * @return void
137
     */
138
    public function setSecondFactorTypes(array $secondFactorTypes)
139
    {
140
        $this->assertAllNonEmptyString($secondFactorTypes, 'secondFactorTypes');
141
142
        $this->secondFactorTypes = $secondFactorTypes;
143
    }
144
145
    /**
146
     * @param string $orderBy
147
     * @return $this
148
     */
149
    public function setOrderBy($orderBy)
150
    {
151
        $this->assertNonEmptyString($orderBy, 'orderBy');
152
153
        $this->orderBy = $orderBy;
154
155
        return $this;
156
    }
157
158
    /**
159
     * @param string $orderDirection
160
     * @return $this
161
     */
162
    public function setOrderDirection($orderDirection)
163
    {
164
        $this->assertNonEmptyString($orderDirection, 'orderDirection');
165
        Assert\that($orderDirection)->choice(
166
            ['asc', 'desc', '', null],
167
            "Invalid order direction, must be one of 'asc', 'desc'"
168
        );
169
170
        $this->orderDirection = $orderDirection;
171
172
        return $this;
173
    }
174
175
    public function toHttpQuery()
176
    {
177
        return '?' . http_build_query(
178
            array_filter(
179
                [
180
                    'actorInstitution'  => $this->actorInstitution,
181
                    'institution'       => $this->institution,
182
                    'commonName'        => $this->commonName,
183
                    'email'             => $this->email,
184
                    'secondFactorTypes' => $this->secondFactorTypes,
185
                    'orderBy'           => $this->orderBy,
186
                    'orderDirection'    => $this->orderDirection,
187
                    'p'                 => $this->pageNumber,
188
                ],
189
                function ($value) {
190
                    return !is_null($value);
191
                }
192
            )
193
        );
194
    }
195
196
    /**
197
     * @param mixed       $value
198
     * @param string      $parameterName
199
     * @param string|null $message
200
     */
201 View Code Duplication
    private function assertNonEmptyString($value, $parameterName, $message = null)
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
        $message = sprintf(
204
            $message ?: '"%s" must be a non-empty string, "%s" given',
205
            $parameterName,
206
            (is_object($value) ? get_class($value) : gettype($value))
207
        );
208
209
        Assert\that($value)->string($message)->notEmpty($message);
210
    }
211
212
    /**
213
     * @param array $values
214
     * @param string $parameterName
215
     *
216
     * @return void
217
     */
218
    private function assertAllNonEmptyString(array $values, $parameterName)
219
    {
220
        foreach ($values as $value) {
221
            $this->assertNonEmptyString(
222
                $value,
223
                $parameterName,
224
                'Elements of "%s" must be non-empty strings, element of type "%s" given'
225
            );
226
        }
227
    }
228
}
229