Test Failed
Push — master ( b976f5...8958fb )
by Alex
02:01
created

Size::valid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
namespace Mezon\Security\Validators\File;
3
4
/**
5
 * Class Size
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 Size implements \Mezon\Security\Validators\ValidatorInterface
14
{
15
    /**
16
     * Bytes in KB
17
     * 
18
     * @var integer
19
     */
20
    public const KB = 1024;
21
22
    /**
23
     * Bytes in MB
24
     *
25
     * @var integer
26
     */
27
    public const MB = 1048576;
28
29
    /**
30
     * Bytes in GB
31
     *
32
     * @var integer
33
     */
34
    public const GB = 1073741824;
35
36
    /**
37
     * Index in the $_FILES array
38
     *
39
     * @var string
40
     */
41
    private $file = '';
42
43
    /**
44
     * Required size in bytes
45
     *
46
     * @var integer
47
     */
48
    private $requiredSize = 0;
49
50
    /**
51
     * Constructor
52
     *
53
     * @param int $size
54
     *            size constraint for the file
55
     * @codeCoverageIgnore
56
     */
57
    public function __construct(int $size)
58
    {
59
        $this->requiredSize = $size;
60
    }
61
62
    /**
63
     *
64
     * {@inheritdoc}
65
     * @see \Mezon\Security\Validators\ValidatorInterface::valid()
66
     */
67
    public function valid(): bool
68
    {
69
        if (isset($_FILES[$this->file]) === false) {
70
            throw (new \Exception('The index "' . $this->file . '" was not found in the $_FILES array', - 1));
71
        }
72
73
        return $_FILES[$this->file]['size'] <= $this->requiredSize;
74
    }
75
76
    /**
77
     *
78
     * {@inheritdoc}
79
     * @see \Mezon\Security\Validators\ValidatorInterface::setValidatingData()
80
     */
81
    public function setValidatingData($data): void
82
    {
83
        $this->file = $data;
84
    }
85
}