ImageMaximumWidthHeight   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 10
dl 0
loc 62
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setValidatingData() 0 3 1
A valid() 0 7 2
1
<?php
2
namespace Mezon\Security\Validators\File;
3
4
use Mezon\Security\Validators\AbstractValidator;
5
6
/**
7
 * Class ImageMaximumWidthHeight
8
 *
9
 * @package Mezon
10
 * @subpackage Security
11
 * @author Dodonov A.A.
12
 * @version v.1.0 (2020/05/13)
13
 * @copyright Copyright (c) 2020, aeon.org
14
 */
15
class ImageMaximumWidthHeight extends AbstractValidator
16
{
17
18
    /**
19
     * Index in the $_FILES array
20
     *
21
     * @var string
22
     */
23
    private $file = '';
24
25
    /**
26
     * Maximum width of the image
27
     *
28
     * @var integer
29
     */
30
    private $maximumWidth = 0;
31
32
    /**
33
     * Maximum height of the image
34
     *
35
     * @var integer
36
     */
37
    private $maximumHeight = 0;
38
39
    /**
40
     * Constructor
41
     *
42
     * @param int $width
43
     *            width constraint for the file
44
     * @param int $height
45
     *            height constraint for the file
46
     * @codeCoverageIgnore
47
     */
48
    public function __construct(int $width, int $height)
49
    {
50
        $this->maximumWidth = $width;
51
52
        $this->maximumHeight = $height;
53
    }
54
55
    /**
56
     *
57
     * {@inheritdoc}
58
     * @see \Mezon\Security\Validators\ValidatorInterface::valid()
59
     */
60
    public function valid(): bool
61
    {
62
        $this->validateFilesFieldExists($this->file);
63
64
        list($width, $height) = getimagesize($_FILES[$this->file]['tmp_name']);
65
66
        return $width <= $this->maximumWidth && $height <= $this->maximumHeight;
67
    }
68
69
    /**
70
     *
71
     * {@inheritdoc}
72
     * @see \Mezon\Security\Validators\ValidatorInterface::setValidatingData()
73
     */
74
    public function setValidatingData($data): void
75
    {
76
        $this->file = $data;
77
    }
78
}