AddonBuilderScreenshotValidator::isValidImage()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
/**
3
 * Validates that an upload is a valid add-on screenshot.
4
 */
5
class AddonBuilderScreenshotValidator extends Upload_Validator
6
{
7
8
    public function __construct()
9
    {
10
        $this->setAllowedExtensions(array('jpg', 'jpeg', 'png'));
11
        $this->setAllowedMaxFileSize(250000);
12
    }
13
14
    public function validate()
15
    {
16
        if (!$this->isValidSize()) {
17
            $this->errors[] = 'The file is too large';
18
            return false;
19
        }
20
21
        if (!$this->isValidExtension()) {
22
            $this->errors[] = 'The file does not have a valid extension';
23
            return false;
24
        }
25
26
        if (!$this->isValidImage()) {
27
            $this->errors[] = 'The file does not appear to be an image';
28
            return false;
29
        }
30
31
        return true;
32
    }
33
34
    public function isValidImage()
35
    {
36
        $size = getimagesize($this->tmpFile['tmp_name']);
37
38
        if (!is_array($size)) {
39
            return false;
40
        }
41
42
        return $size[2] == IMAGETYPE_JPEG || $size[2] == IMAGETYPE_PNG;
43
    }
44
}
45