Passed
Push — 5.x ( 368a22...2c5c24 )
by Enjoys
02:40
created

ExtensionsCheck::parseOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
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 ExtensionsCheck implements UploadCheckInterface
13
{
14
15
16
    private UploadedFileInterface|false $value;
17
    private Ruleable $element;
18
    private array $expectedExtensions;
19
    private ?string $message;
20
21 4
    public function __construct(false|UploadedFileInterface $value, Ruleable $element, string  $expectedExtensions, ?string $message = null)
22
    {
23 4
        $this->value = $value;
24 4
        $this->element = $element;
25 4
        $this->expectedExtensions = \array_map('trim', \explode(",", $expectedExtensions));
26 4
        $this->message = $message;
27
    }
28
29 4
    public function check(): bool
30
    {
31 4
        if ($this->value === false) {
32 1
            return true;
33
        }
34 3
        $extension = pathinfo($this->value->getClientFilename() ?? '', PATHINFO_EXTENSION);
35
36 3
        if (is_null($this->message)) {
37 1
            $this->message = 'Загрузка файлов с расширением .' . $extension . ' запрещена';
0 ignored issues
show
Bug introduced by
Are you sure $extension of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
            $this->message = 'Загрузка файлов с расширением .' . /** @scrutinizer ignore-type */ $extension . ' запрещена';
Loading history...
38
        }
39
40
41 3
        if (!in_array($extension, $this->expectedExtensions)) {
42 2
            $this->element->setRuleError($this->message);
43 2
            return false;
44
        }
45 1
        return true;
46
    }
47
}
48