Completed
Push — master ( 4110ca...d44e25 )
by Nic
02:58
created

RaCandidateSearchQuery::assertAllNonEmptyString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
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 $institution;
30
31
    /**
32
     * @var string
33
     */
34
    private $commonName;
35
36
    /**
37
     * @var string
38
     */
39
    private $email;
40
41
    /**
42
     * @var int
43
     */
44
    private $pageNumber = 1;
45
46
    /**
47
     * @var string
48
     */
49
    private $orderBy;
50
51
    /**
52
     * @var string
53
     */
54
    private $orderDirection;
55
56
    /**
57
     * @var string[]
58
     */
59
    private $secondFactorTypes = [];
60
61
    /**
62
     * @param string $institution
63
     * @param int    $pageNumber
64
     */
65 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...
66
    {
67
        $this->assertNonEmptyString($institution, 'institution');
68
        Assert\that($pageNumber)
69
            ->integer('Page number must be an integer')
70
            ->min(0, 'Page number must be greater than or equal to 1');
71
72
        $this->institution = $institution;
73
        $this->pageNumber  = $pageNumber;
74
    }
75
76
    /**
77
     * @param string $commonName
78
     * @return $this
79
     */
80
    public function setCommonName($commonName)
81
    {
82
        $this->assertNonEmptyString($commonName, 'commonName');
83
84
        $this->commonName = $commonName;
85
86
        return $this;
87
    }
88
89
    /**
90
     * @param string $email
91
     * @return $this
92
     */
93
    public function setEmail($email)
94
    {
95
        $this->assertNonEmptyString($email, 'email');
96
97
        $this->email = $email;
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param array $secondFactorTypes
104
     *
105
     * @return void
106
     */
107
    public function setSecondFactorTypes(array $secondFactorTypes)
108
    {
109
        $this->assertAllNonEmptyString($secondFactorTypes, 'secondFactorTypes');
110
111
        $this->secondFactorTypes = $secondFactorTypes;
112
    }
113
114
    /**
115
     * @param string $orderBy
116
     * @return $this
117
     */
118
    public function setOrderBy($orderBy)
119
    {
120
        $this->assertNonEmptyString($orderBy, 'orderBy');
121
122
        $this->orderBy = $orderBy;
123
124
        return $this;
125
    }
126
127
    /**
128
     * @param string $orderDirection
129
     * @return $this
130
     */
131 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...
132
    {
133
        $this->assertNonEmptyString($orderDirection, 'orderDirection');
134
        Assert\that($orderDirection)->choice(
135
            ['asc', 'desc', '', null],
136
            "Invalid order direction, must be one of 'asc', 'desc'"
137
        );
138
139
        $this->orderDirection = $orderDirection;
140
141
        return $this;
142
    }
143
144
    public function toHttpQuery()
145
    {
146
        return '?' . http_build_query(
147
            array_filter(
148
                [
149
                    'institution'       => $this->institution,
150
                    'commonName'        => $this->commonName,
151
                    'email'             => $this->email,
152
                    'secondFactorTypes' => $this->secondFactorTypes,
153
                    'orderBy'           => $this->orderBy,
154
                    'orderDirection'    => $this->orderDirection,
155
                    'p'                 => $this->pageNumber,
156
                ],
157
                function ($value) {
158
                    return !is_null($value);
159
                }
160
            )
161
        );
162
    }
163
164
    /**
165
     * @param mixed       $value
166
     * @param string      $parameterName
167
     * @param string|null $message
168
     */
169 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...
170
    {
171
        $message = sprintf(
172
            $message ?: '"%s" must be a non-empty string, "%s" given',
173
            $parameterName,
174
            (is_object($value) ? get_class($value) : gettype($value))
175
        );
176
177
        Assert\that($value)->string($message)->notEmpty($message);
178
    }
179
180
    /**
181
     * @param array $values
182
     * @param string $parameterName
183
     *
184
     * @return void
185
     */
186
    private function assertAllNonEmptyString(array $values, $parameterName)
187
    {
188
        foreach ($values as $value) {
189
            $this->assertNonEmptyString(
190
                $value,
191
                $parameterName,
192
                'Elements of "%s" must be non-empty strings, element of type "%s" given'
193
            );
194
        }
195
    }
196
}
197