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

AddonBuilderScreenshotValidator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

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
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