Passed
Push — 5.x ( fdc2ab...368a22 )
by Enjoys
02:42
created

SystemCheck::__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
6
namespace Enjoys\Forms\Rule\UploadCheck;
7
8
9
use Enjoys\Forms\Interfaces\Ruleable;
10
use Psr\Http\Message\UploadedFileInterface;
11
12
final class SystemCheck implements UploadCheckInterface
13
{
14
15
16
    private UploadedFileInterface|false $value;
17
    private Ruleable $element;
18
    private ?string $message;
19
20 4
    public function __construct(false|UploadedFileInterface $value, Ruleable $element, string $message = null)
21
    {
22 4
        $this->value = $value;
23 4
        $this->element = $element;
24 4
        $this->message = $message;
25
    }
26
27 4
    public function check(): bool
28
    {
29 4
        if ($this->value === false) {
30 1
            return true;
31
        }
32
33 3
        if (!in_array($this->value->getError(), array(\UPLOAD_ERR_OK, \UPLOAD_ERR_NO_FILE))) {
34 1
            $this->message =
35 1
                UploadCheckInterface::UPLOAD_ERROR_MESSAGE[$this->value->getError(
36
                )] ?? UploadCheckInterface::UPLOAD_ERROR_MESSAGE['unknown']
37
            ;
38 1
            $this->element->setRuleError($this->message);
39 1
            return false;
40
        }
41 2
        return true;
42
    }
43
}
44