Passed
Push — 5.x ( 368a22...2c5c24 )
by Enjoys
02:40
created

MaxsizeCheck::parseOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace Enjoys\Forms\Rule\UploadCheck;
7
8
9
use ByteUnits\Binary;
10
use Enjoys\Forms\Interfaces\Ruleable;
11
use Psr\Http\Message\UploadedFileInterface;
12
13
final class MaxsizeCheck implements UploadCheckInterface
14
{
15
16
17
    private UploadedFileInterface|false $value;
18
    private Ruleable $element;
19
    private int|string $thresholdSize;
20
    private ?string $message;
21
22 6
    public function __construct(false|UploadedFileInterface $value, Ruleable $element, int|string $thresholdSize, ?string $message = null)
23
    {
24 6
        $this->value = $value;
25 6
        $this->element = $element;
26 6
        $this->thresholdSize = $thresholdSize;
27 6
        $this->message = $message;
28
    }
29
30 6
    public function check(): bool
31
    {
32 6
        if ($this->value === false) {
33 1
            return true;
34
        }
35
36
37 5
        $file_size = $this->value->getSize() ?? 0;
38
39 5
        if (is_null($this->message)) {
40 4
            $this->message = 'Размер файла (' . Binary::bytes($file_size)->format(null, " ") . ')'
41 4
                . ' превышает допустимый размер: ' . Binary::bytes($this->thresholdSize)->format(null, " ");
42
        }
43
44 4
        if ($file_size > $this->thresholdSize) {
45 2
            $this->element->setRuleError($this->message);
46 2
            return false;
47
        }
48 2
        return true;
49
    }
50
51
}
52