Completed
Push — master ( ed3593...2f770f )
by Boy
9s
created

IdentitySearchQuery::toHttpQuery()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 9
nc 8
nop 0
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 IdentitySearchQuery implements HttpQuery
25
{
26
    /**
27
     * @var string
28
     */
29
    private $nameId;
30
31
    /**
32
     * @var string
33
     */
34
    private $institution;
35
36
    /**
37
     * @var string
38
     */
39
    private $email;
40
41
    /**
42
     * @var string
43
     */
44
    private $commonName;
45
46
    /**
47
     * @param string $institution
48
     */
49
    public function __construct($institution)
50
    {
51
        $this->assertNonEmptyString($institution, 'institution');
52
53
        $this->institution = $institution;
54
    }
55
56
    /**
57
     * @param string $commonName
58
     * @return IdentitySearchQuery
59
     */
60
    public function setCommonName($commonName)
61
    {
62
        $this->assertNonEmptyString($commonName, 'commonName');
63
64
        $this->commonName = $commonName;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param string $email
71
     * @return IdentitySearchQuery
72
     */
73
    public function setEmail($email)
74
    {
75
        $this->assertNonEmptyString($email, 'email');
76
77
        $this->email = $email;
78
79
        return $this;
80
    }
81
82
    /**
83
     * @param string $nameId
84
     * @return IdentitySearchQuery
85
     */
86
    public function setNameId($nameId)
87
    {
88
        $this->assertNonEmptyString($nameId, 'nameId');
89
90
        $this->nameId = $nameId;
91
92
        return $this;
93
    }
94
95 View Code Duplication
    private function assertNonEmptyString($value, $name)
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...
96
    {
97
        $message = sprintf(
98
            '"%s" must be a non-empty string, "%s" given',
99
            $name,
100
            (is_object($value) ? get_class($value) : gettype($value))
101
        );
102
103
        Assert\that($value)->string($message)->notEmpty($message);
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function toHttpQuery()
110
    {
111
        $fields = ['institution' => $this->institution];
112
113
        if ($this->commonName) {
114
            $fields['commonName'] = $this->commonName;
115
        }
116
117
        if ($this->email) {
118
            $fields['email'] = $this->email;
119
        }
120
121
        if ($this->nameId) {
122
            $fields['NameID'] = $this->nameId;
123
        }
124
125
        return '?' . http_build_query($fields);
126
    }
127
}
128