SecondFactorAuditLogSearchQuery   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 99
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 9
loc 99
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A setOrderBy() 0 6 1
A setOrderDirection() 9 9 2
A toHttpQuery() 0 12 1
A assertNonEmptyString() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
final class SecondFactorAuditLogSearchQuery implements HttpQuery
25
{
26
    /**
27
     * @var string
28
     */
29
    private $institution;
30
31
    /**
32
     * @var string
33
     */
34
    private $identityId;
35
36
    /**
37
     * @var string|null
38
     */
39
    private $orderBy;
40
41
    /**
42
     * @var string|null
43
     */
44
    private $orderDirection;
45
46
    /**
47
     * @var int
48
     */
49
    private $pageNumber;
50
51
    /**
52
     * @param string $institution
53
     * @param string $identityId
54
     * @param int    $pageNumber
55
     */
56
    public function __construct($institution, $identityId, $pageNumber)
57
    {
58
        $this->assertNonEmptyString($institution, 'institution');
59
        $this->assertNonEmptyString($identityId, 'identityId');
60
        Assert\that($pageNumber)
61
            ->integer('Page number must be an integer')
62
            ->min(0, 'Page number must be greater than or equal to 1');
63
64
        $this->institution = $institution;
65
        $this->identityId = $identityId;
66
        $this->pageNumber = $pageNumber;
67
        $this->orderBy = 'recordedOn';
68
        $this->orderDirection = 'desc';
69
    }
70
71
    /**
72
     * @param string $orderBy
73
     */
74
    public function setOrderBy($orderBy)
75
    {
76
        $this->assertNonEmptyString($orderBy, 'orderBy');
77
78
        $this->orderBy = $orderBy;
79
    }
80
81
    /**
82
     * @param string|null $orderDirection
83
     */
84 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...
85
    {
86
        Assert\that($orderDirection)->choice(
87
            ['asc', 'desc', '', null],
88
            "Invalid order direction, must be one of 'asc', 'desc'"
89
        );
90
91
        $this->orderDirection = $orderDirection ?: null;
92
    }
93
94
    private function assertNonEmptyString($value, $name)
95
    {
96
        $message = sprintf(
97
            '"%s" must be a non-empty string, "%s" given',
98
            $name,
99
            (is_object($value) ? get_class($value) : gettype($value))
100
        );
101
102
        Assert\that($value)->string($message)->notEmpty($message);
103
    }
104
105
    /**
106
     * Return the Http Query string as should be used, MUST include the '?' prefix.
107
     *
108
     * @return string
109
     */
110
    public function toHttpQuery()
111
    {
112
        return '?' . http_build_query(
113
            [
114
                'institution'    => $this->institution,
115
                'identityId'     => $this->identityId,
116
                'orderBy'        => $this->orderBy,
117
                'orderDirection' => $this->orderDirection,
118
                'p'              => $this->pageNumber,
119
            ]
120
        );
121
    }
122
}
123