Validate   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 177
ccs 54
cts 54
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A category() 0 8 2
A quantity() 0 8 1
A quantityFormat() 0 11 2
A quantityBoudaries() 0 16 3
A history() 0 11 2
A historyValidationType() 0 9 2
A historyValidationAccess() 0 13 2
A destination() 0 18 3
1
<?php namespace Simondubois\UnsplashDownloader;
2
3
use InvalidArgumentException;
4
5
class Validate {
6
7
8
    //
9
    // Constants
10
    //
11
12
    /**
13
     * Error codes
14
     */
15
    const ERROR_DESTINATION_NOTDIR     = 1;
16
    const ERROR_DESTINATION_UNWRITABLE = 2;
17
    const ERROR_QUANTITY_NOTNUMERIC    = 3;
18
    const ERROR_QUANTITY_NOTPOSITIVE   = 4;
19
    const ERROR_QUANTITY_TOOHIGH       = 5;
20
    const ERROR_HISTORY_NOTFILE        = 6;
21
    const ERROR_HISTORY_NOTRW          = 7;
22
23
24
25
    //
26
    // Quantity
27
    //
28
29
    /**
30
     * Check validity of a quantity
31
     * @param  string $value Value to validate
32
     * @return int Validated value
33
     */
34 4
    public function quantity($value)
35
    {
36 4
        $value = $this->quantityFormat($value);
37
38 3
        $this->quantityBoudaries($value);
39
40 1
        return $value;
41
    }
42
43
    /**
44
     * Format the quantity to integer
45
     * @param  string $value Parameter value
46
     * @return int               Formatted quantity value
47
     */
48 4
    private function quantityFormat($value)
49
    {
50 4
        if (is_numeric($value) === false) {
51 1
            throw new InvalidArgumentException(
52 1
                'The given quantity ('.$value.') is not numeric.',
53
                self::ERROR_QUANTITY_NOTNUMERIC
54 1
            );
55
        }
56
57 3
        return intval($value);
58
    }
59
60
    /**
61
     * Check the quantity value
62
     * @param int $value Formatted quantity value
63
     */
64 3
    private function quantityBoudaries($value)
65
    {
66 3
        if ($value < 0) {
67 1
            throw new InvalidArgumentException(
68 1
                'The given quantity ('.$value.') is not positive.',
69
                self::ERROR_QUANTITY_NOTPOSITIVE
70 1
            );
71
        }
72
73 2
        if ($value > 100) {
74 1
            throw new InvalidArgumentException(
75 1
                'The given quantity ('.$value.') is too high (should not be greater than 100).',
76
                self::ERROR_QUANTITY_TOOHIGH
77 1
            );
78
        }
79 1
    }
80
81
82
83
84
    //
85
    // History
86
    //
87
88
    /**
89
     * Check validity of the history parameter
90
     * @param  string $value Parameter value
91
     * @return null|string     Validated and formatted history value
92
     */
93 3
    public function history($value)
94
    {
95 3
        if (is_null($value)) {
96 1
            return null;
97
        }
98
99 3
        $this->historyValidationType($value);
100 2
        $this->historyValidationAccess($value);
101
102 1
        return $value;
103
    }
104
105
    /**
106
     * Check if history is not a dir
107
     * @param  string $value Parameter value
108
     */
109 3
    private function historyValidationType($value)
110
    {
111 3
        if (is_dir($value) === true) {
112 1
            throw new InvalidArgumentException(
113 1
                'The given history path ('.$value.') is not a file.',
114
                self::ERROR_HISTORY_NOTFILE
115 1
            );
116
        }
117 2
    }
118
119
    /**
120
     * Check if history is accessible
121
     * @param  string $value Parameter value
122
     */
123 2
    private function historyValidationAccess($value)
124
    {
125 2
        $handle = @fopen($value, 'a+');
126
127 2
        if ($handle === false) {
128 1
            throw new InvalidArgumentException(
129 1
                'The given history path ('.$value.') can not be created or opened for read & write.',
130
                self::ERROR_HISTORY_NOTRW
131 1
            );
132
        }
133
134 1
        fclose($handle);
135 1
    }
136
137
138
139
    //
140
    // Destination
141
    //
142
143
    /**
144
     * Check validity of the destination parameter
145
     * @param  string $value Parameter value
146
     * @return string              Validated and formatted destination value
147
     */
148 4
    public function destination($value)
149
    {
150 4
        if (is_dir($value) === false) {
151 2
            throw new InvalidArgumentException(
152 2
                'The given destination path ('.$value.') is not a directory.',
153
                self::ERROR_DESTINATION_NOTDIR
154 2
            );
155
        }
156
157 2
        if (is_writable($value) === false) {
158 1
            throw new InvalidArgumentException(
159 1
                'The given destination path ('.$value.') is not writable.',
160
                self::ERROR_DESTINATION_UNWRITABLE
161 1
            );
162
        }
163
164 1
        return $value;
165
    }
166
167
    /**
168
     * Check validity of the category parameter
169
     * @param  string|null $value Parameter value
170
     * @return int|null Validated and formatted category value
171
     */
172 1
    public function category($value)
173
    {
174 1
        if (is_null($value)) {
175 1
            return null;
176
        }
177
178 1
        return intval($value);
179
    }
180
181
}
182