Passed
Push — master ( 34f20f...a12a6d )
by Enjoys
57s queued 12s
created

MaxsizeCheck::check()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

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