Passed
Pull Request — master (#28)
by Enjoys
21:42 queued 16:27
created

SystemCheck   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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