Completed
Push — master ( 2a99e3...fba018 )
by
unknown
02:29 queued 43s
created

RaSecondFactorExportQuery::getFileName()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 4
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 DateTime;
23
use Surfnet\StepupMiddlewareClient\Dto\HttpQuery;
24
25
final class RaSecondFactorExportQuery implements HttpQuery
26
{
27
    const STATUS_UNVERIFIED = 'unverified';
28
    const STATUS_VERIFIED = 'verified';
29
    const STATUS_VETTED = 'vetted';
30
    const STATUS_REVOKED = 'revoked';
31
32
    /**
33
     * @var string
34
     */
35
    private $institution;
36
37
    /**
38
     * @var string|null
39
     */
40
    private $name;
41
42
    /**
43
     * @var string|null
44
     */
45
    private $type;
46
47
    /**
48
     * @var string|null The second factor type's ID (eg. Yubikey public ID)
49
     */
50
    private $secondFactorId;
51
52
    /**
53
     * @var string|null
54
     */
55
    private $email;
56
57
    /**
58
     * @var string|null One of the STATUS_* constants.
59
     */
60
    private $status;
61
62
    /**
63
     * @var string|null
64
     */
65
    private $orderBy;
66
67
    /**
68
     * @var string|null
69
     */
70
    private $orderDirection;
71
72
    /**
73
     * @param string $institution
74
     */
75
    public function __construct($institution)
76
    {
77
        $this->assertNonEmptyString($institution, 'institution');
78
        $this->institution = $institution;
79
    }
80
81
    public function getFileName()
82
    {
83
        $date = new DateTime();
84
        $date = $date->format('Y-m-d');
85
86
        $extension = '.csv';
87
        $fileName = "token_export_{$date}";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $date instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
88
89
        if ($this->type) {
90
            $fileName .= "-{$this->type}";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
91
        }
92
93
        if ($this->status) {
94
            $fileName .= "-{$this->status}";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
95
        }
96
        return $fileName . $extension;
97
    }
98
99
    /**
100
     * @return null|string
101
     */
102
    public function getName()
103
    {
104
        return $this->name;
105
    }
106
107
    /**
108
     * @param null|string $name
109
     */
110
    public function setName($name)
111
    {
112
        $this->assertNonEmptyString($name, 'name');
113
114
        $this->name = $name;
115
    }
116
117
    /**
118
     * @return null|string
119
     */
120
    public function getType()
121
    {
122
        return $this->type;
123
    }
124
125
    /**
126
     * @param null|string $type
127
     */
128
    public function setType($type)
129
    {
130
        $this->assertNonEmptyString($type, 'type');
131
132
        $this->type = $type;
133
    }
134
135
    /**
136
     * @return null|string
137
     */
138
    public function getSecondFactorId()
139
    {
140
        return $this->secondFactorId;
141
    }
142
143
    /**
144
     * @param null|string $secondFactorId
145
     */
146
    public function setSecondFactorId($secondFactorId)
147
    {
148
        $this->assertNonEmptyString($secondFactorId, 'secondFactorId');
149
150
        $this->secondFactorId = $secondFactorId;
151
    }
152
153
    /**
154
     * @return null|string
155
     */
156
    public function getEmail()
157
    {
158
        return $this->email;
159
    }
160
161
    /**
162
     * @param null|string $email
163
     */
164
    public function setEmail($email)
165
    {
166
        $this->assertNonEmptyString($email, 'email');
167
168
        $this->email = $email;
169
    }
170
171
    /**
172
     * @return null|string
173
     */
174
    public function getStatus()
175
    {
176
        return $this->status;
177
    }
178
179
    /**
180
     * @param string $status
181
     */
182 View Code Duplication
    public function setStatus($status)
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...
183
    {
184
        Assert\that($status)->choice(
185
            [self::STATUS_UNVERIFIED, self::STATUS_VERIFIED, self::STATUS_VETTED, self::STATUS_REVOKED, ''],
186
            'Invalid second factor status, must be one of the STATUS constants'
187
        );
188
189
        $this->status = $status ?: null;
190
    }
191
192
    /**
193
     * @param string $orderBy
194
     */
195
    public function setOrderBy($orderBy)
196
    {
197
        $this->assertNonEmptyString($orderBy, 'orderBy');
198
199
        $this->orderBy = $orderBy;
200
    }
201
202
    /**
203
     * @param string|null $orderDirection
204
     */
205 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...
206
    {
207
        Assert\that($orderDirection)->choice(
208
            ['asc', 'desc', '', null],
209
            "Invalid order direction, must be one of 'asc', 'desc'"
210
        );
211
212
        $this->orderDirection = $orderDirection ?: null;
213
    }
214
215 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...
216
    {
217
        $message = sprintf(
218
            '"%s" must be a non-empty string, "%s" given',
219
            $name,
220
            (is_object($value) ? get_class($value) : gettype($value))
221
        );
222
223
        Assert\that($value)->string($message)->notEmpty($message);
224
    }
225
226
    /**
227
     * Return the Http Query string as should be used, MUST include the '?' prefix.
228
     *
229
     * @return string
230
     */
231 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...
232
    {
233
        return '?' . http_build_query(
234
                array_filter(
235
                    [
236
                        'institution'    => $this->institution,
237
                        'name'           => $this->name,
238
                        'type'           => $this->type,
239
                        'secondFactorId' => $this->secondFactorId,
240
                        'email'          => $this->email,
241
                        'status'         => $this->status,
242
                        'orderBy'        => $this->orderBy,
243
                        'orderDirection' => $this->orderDirection
244
                    ],
245
                    function ($value) {
246
                        return !is_null($value);
247
                    }
248
                )
249
            );
250
    }
251
}
252