Completed
Push — feature/ss4-upgrade ( f41a3f )
by
unknown
10:12
created

AddonBuilderScreenshotValidator::validate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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