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

RequiredCheck::check()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
cc 4
nc 4
nop 0
crap 4
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