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

SystemCheck   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 16
c 1
b 0
f 0
dl 0
loc 30
ccs 13
cts 14
cp 0.9286
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A check() 0 15 3
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