Completed
Push — master ( 458234...a0327e )
by Adrian
01:39
created

Size::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
declare(strict_types=1);
3
namespace Sirius\Validation\Rule\File;
4
5
use Sirius\Validation\Rule\AbstractRule;
6
use Sirius\Validation\Util\RuleHelper;
7
8
class Size extends AbstractRule
9
{
10
    const OPTION_SIZE = 'size';
11
12
    const MESSAGE = 'The file should not exceed {size}';
13
14
    const LABELED_MESSAGE = '{label} should not exceed {size}';
15
16
    protected $options = [
17
        self::OPTION_SIZE => '2M'
18 2
    ];
19
20 2
    public function validate($value, string $valueIdentifier = null)
21 2
    {
22 2
        $this->value = $value;
23 1
        if (! file_exists($value)) {
24 1
            $this->success = false;
25 1
        } else {
26 1
            $fileSize      = @filesize($value);
27
            $limit         = RuleHelper::normalizeFileSize($this->options[self::OPTION_SIZE]);
28
            $this->success = $fileSize && $fileSize <= $limit;
29 2
        }
30
31
        return $this->success;
32 3
    }
33
}
34