Completed
Push — release-1.x ( 6974c9...f5fa1d )
by Boy
10s
created

RaCandidateSearchQuery::setEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
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 $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
     * @param string $institution
58
     * @param int    $pageNumber
59
     */
60 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...
61
    {
62
        $this->assertNonEmptyString($institution, 'institution');
63
        Assert\that($pageNumber)
64
            ->integer('Page number must be an integer')
65
            ->min(0, 'Page number must be greater than or equal to 1');
66
67
        $this->institution = $institution;
68
        $this->pageNumber  = $pageNumber;
69
    }
70
71
    /**
72
     * @param string $commonName
73
     * @return $this
74
     */
75
    public function setCommonName($commonName)
76
    {
77
        $this->assertNonEmptyString($commonName, 'commonName');
78
79
        $this->commonName = $commonName;
80
81
        return $this;
82
    }
83
84
    /**
85
     * @param string $email
86
     * @return $this
87
     */
88
    public function setEmail($email)
89
    {
90
        $this->assertNonEmptyString($email, 'email');
91
92
        $this->email = $email;
93
94
        return $this;
95
    }
96
97
    /**
98
     * @param string $orderBy
99
     * @return $this
100
     */
101
    public function setOrderBy($orderBy)
102
    {
103
        $this->assertNonEmptyString($orderBy, 'orderBy');
104
105
        $this->orderBy = $orderBy;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @param string $orderDirection
112
     * @return $this
113
     */
114 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...
115
    {
116
        $this->assertNonEmptyString($orderDirection, 'orderDirection');
117
        Assert\that($orderDirection)->choice(
118
            ['asc', 'desc', '', null],
119
            "Invalid order direction, must be one of 'asc', 'desc'"
120
        );
121
122
        $this->orderDirection = $orderDirection;
123
124
        return $this;
125
    }
126
127
    public function toHttpQuery()
128
    {
129
        return '?' . http_build_query(
130
            array_filter(
131
                [
132
                    'institution'    => $this->institution,
133
                    'commonName'     => $this->commonName,
134
                    'email'          => $this->email,
135
                    'orderBy'        => $this->orderBy,
136
                    'orderDirection' => $this->orderDirection,
137
                    'p'              => $this->pageNumber,
138
                ],
139
                function ($value) {
140
                    return !is_null($value);
141
                }
142
            )
143
        );
144
    }
145
146
    /**
147
     * @param mixed  $value
148
     * @param string $parameterName
149
     */
150 View Code Duplication
    private function assertNonEmptyString($value, $parameterName)
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...
151
    {
152
        $message = sprintf(
153
            '"%s" must be a non-empty string, "%s" given',
154
            $parameterName,
155
            (is_object($value) ? get_class($value) : gettype($value))
156
        );
157
158
        Assert\that($value)->string($message)->notEmpty($message);
159
    }
160
}
161