AudioValidator::validateMinDuration()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
rs 9.52
cc 4
nc 4
nop 2
1
<?php
2
3
namespace Toa\Component\Validator\Constraints;
4
5
use Symfony\Component\Validator\Constraint;
6
use Symfony\Component\Validator\Constraints\FileValidator;
7
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
8
use Toa\Component\Validator\Exception\ProviderException;
9
use Toa\Component\Validator\Provider\AudioProviderInterface;
10
11
/**
12
 * Class AudioValidator
13
 *
14
 * @author Enrico Thies <[email protected]>
15
 */
16
class AudioValidator extends FileValidator
17
{
18
    /** @var ProviderInterface */
19
    protected $provider;
20
21
    /**
22
     * @param AudioProviderInterface $provider
23
     */
24
    public function __construct(AudioProviderInterface $provider)
25
    {
26
        $this->provider = $provider;
0 ignored issues
show
Documentation Bug introduced by
It seems like $provider of type object<Toa\Component\Val...AudioProviderInterface> is incompatible with the declared type object<Toa\Component\Val...ints\ProviderInterface> of property $provider.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
    }
28
29
    /**
30
     * {@inheritDoc}
31
     */
32
    public function validate($value, Constraint $constraint)
33
    {
34
        $violations = count($this->context->getViolations());
35
36
        parent::validate($value, $constraint);
37
38
        $failed = count($this->context->getViolations()) !== $violations;
39
40
        if ($failed || null === $value || '' === $value) {
41
            return;
42
        }
43
44
        $path = $value instanceof \SplFileInfo ? $value->getPathname() : (string) $value;
45
46
        try {
47
            $duration = $this->provider->getDuration($path);
48
        } catch (ProviderException $e) {
49
            $this->context->addViolation($constraint->formatNotDetectedMessage);
50
51
            return;
52
        }
53
54
        if ($this->validateMaxDuration($duration, $constraint)) {
55
            return;
56
        }
57
58
        if ($this->validateMinDuration($duration, $constraint)) {
59
            return;
60
        }
61
    }
62
63
    /**
64
     * @param float      $duration
65
     * @param Constraint $constraint
66
     *
67
     * @throws ConstraintDefinitionException
68
     * @return boolean
69
     */
70
    protected function validateMaxDuration($duration, Constraint $constraint)
71
    {
72
        if ($constraint->maxDuration) {
73
            if (!ctype_digit((string) $constraint->maxDuration)) {
74
                throw new ConstraintDefinitionException(
75
                    sprintf(
76
                        '"%s" is not a valid maximum duration',
77
                        $constraint->maxDuration
78
                    )
79
                );
80
            }
81
82
            if ($duration > $constraint->maxDuration) {
83
                $this->context->addViolation(
84
                    $constraint->maxDurationMessage,
85
                    array(
86
                        '{{ duration }}' => $duration,
87
                        '{{ max_duration }}' => $constraint->maxDuration
88
                    )
89
                );
90
91
                return true;
92
            }
93
        }
94
    }
95
96
    /**
97
     * @param float      $duration
98
     * @param Constraint $constraint
99
     *
100
     * @throws ConstraintDefinitionException
101
     * @return boolean
102
     */
103
    protected function validateMinDuration($duration, Constraint $constraint)
104
    {
105
        if ($constraint->minDuration) {
106
            if (!ctype_digit((string) $constraint->minDuration)) {
107
                throw new ConstraintDefinitionException(
108
                    sprintf(
109
                        '"%s" is not a valid minimum duration',
110
                        $constraint->minDuration
111
                    )
112
                );
113
            }
114
115
            if ($duration < $constraint->minDuration) {
116
                $this->context->addViolation(
117
                    $constraint->minDurationMessage,
118
                    array(
119
                        '{{ duration }}' => $duration,
120
                        '{{ min_duration }}' => $constraint->minDuration
121
                    )
122
                );
123
124
                return true;
125
            }
126
        }
127
    }
128
}
129