Passed
Push — scrutinizer ( 3b62b8...241763 )
by
unknown
08:40 queued 52s
created

Validator::validId()   C

Complexity

Conditions 16
Paths 16

Size

Total Lines 66
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 20.7479

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 16
eloc 34
c 3
b 0
f 0
nc 16
nop 2
dl 0
loc 66
ccs 25
cts 34
cp 0.7352
crap 20.7479
rs 5.5666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace InShore\Bookwhen;
6
7
use InShore\Bookwhen\Interfaces\ValidatorInterface;
8
use Respect\Validation\Validator as v;
9
10
class Validator implements ValidatorInterface
11
{
12
    
13
    /**
14
     * 
15
     */
16 74
    public function __construct()
17
    {
18
        
19 74
    }
20
    
21
    /**
22
     * 
23
     * {@inheritDoc}
24
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validDate()
25
     */
26 14
    public function validDate($date): bool 
27
    {
28 14
        if (v::stringType()->notEmpty()->numericVal()->length(8, 8)->date('Ymd')->validate($date)) {
29 8
            return true;
30
        } else { 
31 6
            return v::stringType()->notEmpty()->numericVal()->length(14, 14)->dateTime('YmdHis')->validate($date);
32
        }
33
    }
34
35
    /**
36
     * 
37
     * {@inheritDoc}
38
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validFileName()
39
     */
40 4
    public function validFileName($fileName): bool
41
    {
42 4
        return v::stringType()->notEmpty()->validate($fileName);
43
    }
44
45
    /**
46
     * 
47
     * {@inheritDoc}
48
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validFileType()
49
     */
50 2
    public function validFileType($fileType): bool
51
    {
52 2
        return v::stringType()->notEmpty()->in(['jpg', 'jpeg', 'gif', 'png'])->validate(strtolower($fileType));
53
    }
54
    
55
    /**
56
     * 
57
     * {@inheritDoc}
58
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validFrom()
59
     */
60 3
    public function validFrom($from, $to = null): bool 
61
    { 
62 3
        if (!$this->validDate($from)) {
63 3
            return false;
64
        }
65
        
66
        $fromDate = new \DateTime($from);
67
        
68
        if (empty($to)) {
69
            return true;
70
        }
71
        
72
        if (!$this->validDate($to)) {
73
            return false;
74
        }
75
        $toDate = new \DateTime($to);
76
        
77
        // Compare if actual to date is greater than from.
78
        if ($fromDate > $toDate) {
79
            return false;
80
        }
81
        
82
        return true;
83
    }
84
85
    /**
86
     * 
87
     * {@inheritDoc}
88
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validId()
89
     * @todo
90
     */
91 28
    public function validId($Id, $type = null): bool 
92
    {
93 28
        if (!v::stringType()->notEmpty()->validate($Id)) {
94 13
            return false;
95
        }
96
97 15
        switch ($type) {
98 15
            case 'classPass':
99
100 1
                $exploded = explode('-', $Id);
101
                
102 1
                if (count($exploded) !== 2) {
103
                    return false;
104
                }
105
106 1
                if ($exploded[0] !== 'cp') {
107
                    return false;
108
                }
109
110 1
                return v::stringType()->notEmpty()->alnum()->length(12, 12)->validate($exploded[1]);
111
112 14
            case 'event':
113
                
114 1
                $exploded = explode('-', $Id);
115
                
116 1
                if (count($exploded) !== 3) {
117
                    return false;
118
                }
119
                
120 1
                if ($exploded[0] !== 'ev') {
121
                    return false;
122
                }
123
                
124
                // Syntax.
125 1
                if (!v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[1])) {
126
                    return false;
127
                }
128
                
129 1
                return $this->validDate($exploded[2]);
130
            
131 13
            case 'ticket':
132 1
                $exploded = explode('-', $Id);
133
                
134 1
                if (count($exploded) !== 4) {
135
                    return false;
136
                }
137
                
138 1
                if ($exploded[0] !== 'ti') {
139
                    return false;
140
                }
141
                
142
                // Syntax.
143 1
                if (!v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[1])) {
144
                    return false;
145
                }
146
                
147 1
                if (!$this->validDate($exploded[2])) {
148
                    return false;
149
                }
150
                
151 1
                return v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[3]);
152
            
153 12
            case 'attachment':
154 8
            case 'location':
155
            default:
156 12
                return v::alnum()->length(12, 12)->validate($Id);
157
        }
158
    } 
159
160
    /**
161
     * 
162
     * {@inheritDoc}
163
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validInclude()
164
     */
165 4
    public function validInclude($include): bool
166
    {
167 4
        return v::boolType()->validate($include);
168
    }
169
170
    /**
171
     * 
172
     * {@inheritDoc}
173
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validTag()
174
     */
175 11
    public function validTag($tag): bool 
176
    {
177 11
        return v::stringType()->notEmpty()->alnum()->validate($tag);
178
    }
179
180
    /**
181
     * 
182
     * {@inheritDoc}
183
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validTitle()
184
     */
185 4
    public function validTitle($title): bool
186
    {
187 4
        return v::stringType()->notEmpty()->validate($title);
188
    }
189
    
190
    /**
191
     * 
192
     * {@inheritDoc}
193
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validTo()
194
     */
195 5
    public function validTo($to, $from = null): bool 
196
    {        
197 5
        if (!$this->validDate($to)) {
198
            return false;
199
        }
200
201 5
        $toDate = new \DateTime($to);
202
        
203 5
        if (empty($from)) {
204 5
            return true;
205
        }
206
        
207
        $fromDate = new \DateTime($from);
208
        if (!$this->validFrom($from)) {
209
            return false;
210
        }
211
        if ($toDate < $fromDate) {
212
            return false;
213
        }
214
        
215
        return true;
216
    }
217
    
218
    /**
219
     * 
220
     * {@inheritDoc}
221
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validToken()
222
     */
223 1
    public function validToken($token): bool
224
    {
225 1
        return v::alnum()->validate($token);
226
    }
227
}
228
229
// EOF!
230