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

ExtensionsCheck   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 18
c 1
b 0
f 0
dl 0
loc 32
ccs 15
cts 15
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 17 4
A __construct() 0 6 1
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