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

RequiredCheck::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms\Rule\UploadCheck;
6
7
use Enjoys\Forms\Interfaces\Ruleable;
8
use Psr\Http\Message\UploadedFileInterface;
9
10
final class RequiredCheck implements UploadCheckInterface
11
{
12
    private UploadedFileInterface|false $value;
13
    private Ruleable $element;
14
    private ?string $message;
15
16 6
    public function __construct(false|UploadedFileInterface $value, Ruleable $element, string $message = null)
17
    {
18 6
        $this->value = $value;
19 6
        $this->element = $element;
20 6
        $this->message = $message;
21
    }
22
23 6
    public function check(): bool
24
    {
25 6
        if (is_null($this->message)) {
26 5
            $this->message = 'Выберите файл для загрузки';
27
        }
28
29 6
        if ($this->value === false || $this->value->getError() == \UPLOAD_ERR_NO_FILE) {
30 3
            $this->element->setRuleError($this->message);
31 3
            return false;
32
        }
33
34 3
        return true;
35
    }
36
}
37