Completed
Push — master ( d45ca1...554243 )
by Adrian
01:42
created

Size   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 28
ccs 9
cts 10
cp 0.9
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
    ];
19
20 4
    public function validate($value, string $valueIdentifier = null)
21
    {
22 4
        $this->value = $value;
23 4
        if (! is_array($value) || ! isset($value['tmp_name'])) {
24
            $this->success = false;
25 4
        } elseif (! file_exists($value['tmp_name'])) {
26 2
            $this->success = $value['error'] === UPLOAD_ERR_NO_FILE;
27
        } else {
28 2
            $fileSize      = @filesize($value['tmp_name']);
29 2
            $limit         = RuleHelper::normalizeFileSize($this->options[self::OPTION_SIZE]);
30 2
            $this->success = $fileSize && $fileSize <= $limit;
31
        }
32
33 4
        return $this->success;
34
    }
35
}
36