Validator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 5
eloc 8
dl 0
loc 38
rs 10
c 4
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 8 3
A addConstraint() 0 3 1
1
<?php
2
3
namespace Slince\Upload;
4
5
use Slince\Upload\Constraint\ConstraintInterface;
6
use Slince\Upload\Exception\ConstraintException;
7
use Symfony\Component\HttpFoundation\File\UploadedFile;
8
9
final class Validator
10
{
11
    /**
12
     * Array of constraints
13
     *
14
     * @var ConstraintInterface[]
15
     */
16
    protected $constraints = [];
17
18
    public function __construct(array $constraints = [])
19
    {
20
        $this->constraints = $constraints;
21
    }
22
23
    /**
24
     * Add a constraint
25
     * @param ConstraintInterface $constraint
26
     */
27
    public function addConstraint(ConstraintInterface $constraint): void
28
    {
29
        $this->constraints[] = $constraint;
30
    }
31
32
    /**
33
     * Validate the file
34
     *
35
     * @param UploadedFile $file
36
     * @return true
37
     * @throws ConstraintException
38
     */
39
    public function validate(UploadedFile $file): bool
40
    {
41
        foreach ($this->constraints as $constraint) {
42
            if (!$constraint->validate($file)) {
43
                throw new ConstraintException($constraint, $file);
44
            }
45
        }
46
        return true;
47
    }
48
}
49