FileValidator::getErrors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the IrishDan\ResponsiveImageBundle package.
4
 *
5
 * (c) Daniel Byrne <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source
8
 * code.
9
 */
10
11
namespace IrishDan\ResponsiveImageBundle\File;
12
13
use Symfony\Component\HttpFoundation\File\UploadedFile;
14
15
/**
16
 * Class FileValidator
17
 *
18
 * @package IrishDan\ResponsiveImageBundle\File
19
 */
20
class FileValidator implements FileValidatorInterface
21
{
22
    protected $errors;
23
    protected $allowedTypes = [
24
        'jpeg',
25
        'jpg',
26
        'png',
27
        'gif',
28
    ];
29
30
    /**
31
     * @param UploadedFile $file
32
     *
33
     * @return bool
34
     */
35
    public function validate(UploadedFile $file)
36
    {
37
        // Check allowed types.
38
        $fileExtension        = strtolower($file->getClientOriginalExtension());
39
        $guessedFileExtension = strtolower($file->guessExtension());
40
41
        if (!in_array($guessedFileExtension, $this->allowedTypes)) {
42
          $this->errors[] = 'Files of "' . $guessedFileExtension . '" type are not allowed';
43
44
          return false;
45
        }
46
47
        if (!in_array($fileExtension, $this->allowedTypes)) {
48
            $this->errors[] = 'Files of "' . $fileExtension . '" type are not allowed';
49
50
            return false;
51
        }
52
53
        return true;
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public function getAllowedTypes()
60
    {
61
        return $this->allowedTypes;
62
    }
63
64
    /**
65
     * @param array $allowedTypes
66
     */
67
    public function setAllowedTypes($allowedTypes)
68
    {
69
        $this->allowedTypes = $allowedTypes;
70
    }
71
72
    /**
73
     * @return mixed
74
     */
75
    public function getErrors()
76
    {
77
        return $this->errors;
78
    }
79
}