Test Failed
Push — master ( e3b206...99d1d4 )
by Enjoys
02:23 queued 23s
created

MaxsizeCheck   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 19
c 1
b 0
f 0
dl 0
loc 38
ccs 0
cts 16
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 19 4
A __construct() 0 10 1
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
    public function __construct(
19
        false|UploadedFileInterface $value,
20
        Ruleable $element,
21
        int|string $thresholdSize,
22
        ?string $message = null
23
    ) {
24
        $this->value = $value;
25
        $this->element = $element;
26
        $this->thresholdSize = $thresholdSize;
27
        $this->message = $message;
28
    }
29
30
    public function check(): bool
31
    {
32
        if ($this->value === false) {
33
            return true;
34
        }
35
36
37
        $file_size = $this->value->getSize() ?? 0;
38
39
        if (is_null($this->message)) {
40
            $this->message = 'Размер файла (' . Binary::bytes($file_size)->format(null, " ") . ')'
41
                . ' превышает допустимый размер: ' . Binary::bytes($this->thresholdSize)->format(null, " ");
42
        }
43
44
        if ($file_size > $this->thresholdSize) {
45
            $this->element->setRuleError($this->message);
46
            return false;
47
        }
48
        return true;
49
    }
50
}
51