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

Size   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 28
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 15 5
1
<?php
2
declare(strict_types=1);
3
namespace Sirius\Validation\Rule\Upload;
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 (! is_array($value) || ! isset($value['tmp_name'])) {
24 1
            $this->success = false;
25 1
        } elseif (! file_exists($value['tmp_name'])) {
26 1
            $this->success = $value['error'] === UPLOAD_ERR_NO_FILE;
27
        } else {
28
            $fileSize      = @filesize($value['tmp_name']);
29 2
            $limit         = RuleHelper::normalizeFileSize($this->options[self::OPTION_SIZE]);
30
            $this->success = $fileSize && $fileSize <= $limit;
31
        }
32 4
33
        return $this->success;
34 4
    }
35
}
36