Test Failed
Push — master ( e1ef6d...0e97a6 )
by Alex
01:49
created

ImageMaximumWidthHeight::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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