Test Failed
Push — master ( 8ab05d...e1ef6d )
by Alex
01:42
created

MimeType::setValidatingData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
namespace Mezon\Security\Validators\File;
3
4
/**
5
 * Class MimeType
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 MimeType implements \Mezon\Security\Validators\ValidatorInterface
14
{
15
16
    /**
17
     * Index in the $_FILES array
18
     *
19
     * @var string
20
     */
21
    private $file = '';
22
23
    /**
24
     * Available mime types
25
     *
26
     * @var array
27
     */
28
    private $requiredTypes = [];
29
30
    /**
31
     * Constructor
32
     *
33
     * @param array $types
34
     *            mime type constraint for the file
35
     * @codeCoverageIgnore
36
     */
37
    public function __construct(array $types)
38
    {
39
        $this->requiredTypes = $types;
40
    }
41
42
    /**
43
     *
44
     * {@inheritdoc}
45
     * @see \Mezon\Security\Validators\ValidatorInterface::valid()
46
     */
47
    public function valid(): bool
48
    {
49
        if (isset($_FILES[$this->file]) === false) {
50
            throw (new \Exception('The index "' . $this->file . '" was not found in the $_FILES array', - 1));
51
        }
52
53
        return in_array(mime_content_type($_FILES[$this->file]['tmp_name']), $this->requiredTypes);
54
    }
55
56
    /**
57
     *
58
     * {@inheritdoc}
59
     * @see \Mezon\Security\Validators\ValidatorInterface::setValidatingData()
60
     */
61
    public function setValidatingData($data): void
62
    {
63
        $this->file = $data;
64
    }
65
}