Completed
Push — master ( fe0371...1946fd )
by Simon
04:01
created

Validate   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

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