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

ExtensionsCheck::check()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.9666
cc 4
nc 5
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 ExtensionsCheck implements UploadCheckInterface
11
{
12
    private UploadedFileInterface|false $value;
13
    private Ruleable $element;
14
    private array $expectedExtensions;
15
    private ?string $message;
16
17 6
    public function __construct(false|UploadedFileInterface $value, Ruleable $element, string $expectedExtensions, ?string $message = null)
18
    {
19 6
        $this->value = $value;
20 6
        $this->element = $element;
21 6
        $this->expectedExtensions = \array_map('trim', \explode(",", $expectedExtensions));
22 6
        $this->message = $message;
23
    }
24
25 6
    public function check(): bool
26
    {
27 6
        if ($this->value === false) {
28 1
            return true;
29
        }
30 5
        $extension = pathinfo($this->value->getClientFilename() ?? '', PATHINFO_EXTENSION);
31
32 5
        if (is_null($this->message)) {
33 3
            $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

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