AddonBuilderScreenshotValidator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 40
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A validate() 0 19 4
A isValidImage() 0 10 3
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