Whitelist   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 32
dl 0
loc 90
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 9 5
A allowedExtensions() 0 5 1
A video() 0 18 1
A pdf() 0 12 1
A allowedMimeTypes() 0 5 1
1
<?php
2
3
/*
4
 * This file is part of the Scrawler package.
5
 *
6
 * (c) Pranjal Pandey <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Scrawler\Validator\Storage;
13
14
use Symfony\Component\HttpFoundation\File\File;
15
use Symfony\Component\HttpFoundation\File\UploadedFile;
16
17
class Whitelist extends AbstractValidator
18
{
19
    /**
20
     * @var array<string>
21
     */
22
    protected array $allowedMimeTypes = [];
23
    /**
24
     * @var array<string>
25
     */
26
    protected array $allowedExtensions = [];
27
28
    /**
29
     * Set the allowed mime types.
30
     *
31
     * @param array<string> $allowedMimeTypes
32
     */
33
    public function allowedMimeTypes(array $allowedMimeTypes): self
34
    {
35
        $this->allowedMimeTypes = $allowedMimeTypes;
36
37
        return $this;
38
    }
39
40
    /**
41
     * Set the allowed extensions.
42
     *
43
     * @param array<string> $allowedExtensions
44
     */
45
    public function allowedExtensions(array $allowedExtensions): self
46
    {
47
        $this->allowedExtensions = $allowedExtensions;
48
49
        return $this;
50
    }
51
52
    /**
53
     * Validate the uploaded file.
54
     *
55
     * @throws \Scrawler\Exception\FileValidationException
56
     */
57
    #[\Override]
58
    public function validate(UploadedFile|File $file): void
59
    {
60
        if ([] !== $this->allowedMimeTypes && !\in_array($file->getMimeType(), $this->allowedMimeTypes, true)) {
61
            throw new \Scrawler\Exception\FileValidationException('Invalid file type.');
62
        }
63
64
        if ([] !== $this->allowedExtensions && !\in_array($file->guessExtension(), $this->allowedExtensions, true)) {
65
            throw new \Scrawler\Exception\FileValidationException('Invalid file extension.');
66
        }
67
    }
68
69
    /**
70
     * Pdf whitelist for pdf with 5MB max file size.
71
     */
72
    public static function pdf(): self
73
    {
74
        $whitelist = new self();
75
        $whitelist->allowedMimeTypes([
76
            'application/pdf',
77
        ]);
78
        $whitelist->allowedExtensions([
79
            'pdf',
80
        ]);
81
        $whitelist->maxSize(1024 * 1024 * 5);
82
83
        return $whitelist;
84
    }
85
86
    /**
87
     * Video whitelist for mp4, mov, mpeg, webm with 20MB max file size.
88
     */
89
    public static function video(): self
90
    {
91
        $whitelist = new self();
92
        $whitelist->allowedMimeTypes([
93
            'video/mp4',
94
            'video/quicktime',
95
            'video/mpeg',
96
            'video/webm',
97
        ]);
98
        $whitelist->allowedExtensions([
99
            'mp4',
100
            'mov',
101
            'mpeg',
102
            'webm',
103
        ]);
104
        $whitelist->maxSize(1024 * 1024 * 20);
105
106
        return $whitelist;
107
    }
108
}
109