ExtensionConstraint   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
eloc 7
c 3
b 0
f 0
dl 0
loc 40
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrorMessage() 0 3 1
A validate() 0 3 1
A setErrorMessage() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
namespace Slince\Upload\Constraint;
4
5
use Symfony\Component\HttpFoundation\File\UploadedFile;
6
7
class ExtensionConstraint implements ConstraintInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $errorMessageTemplate = 'File extension {extension} is invalid';
13
14
    /**
15
     * allowed extensions
16
     * @var array
17
     */
18
    protected $allowedExtensions = [];
19
20
    public function __construct(array $allowedExtensions)
21
    {
22
        $this->allowedExtensions = array_map('strtolower', $allowedExtensions);
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function validate(UploadedFile $file): bool
29
    {
30
        return in_array(strtolower($file->getClientOriginalExtension()), $this->allowedExtensions, true);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getErrorMessage(UploadedFile $file): string
37
    {
38
        return str_replace('{extension}', $file->getClientOriginalExtension(), $this->errorMessageTemplate);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function setErrorMessage(string $messageTemplate): void
45
    {
46
        $this->errorMessageTemplate = $messageTemplate;
47
    }
48
}
49