GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

MediaType::check()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 24
ccs 12
cts 12
cp 1
rs 9.6111
cc 5
nc 5
nop 1
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Upload\Rule;
6
7
use Enjoys\Upload\Exception\RuleException;
8
use Enjoys\Upload\RuleInterface;
9
use Psr\Http\Message\UploadedFileInterface;
10
11
final class MediaType implements RuleInterface
12
{
13
14
15
    private array $allowedMediaType = [];
16
    private string $errorMessage;
17
    private bool $allowedAllMediaType = false;
18
19 16
    public function __construct(string $errorMessage = null)
20
    {
21 16
        $this->errorMessage = $errorMessage ?? 'Media type is disallow: `%s`';
22
    }
23
24 7
    public function check(UploadedFileInterface $file): void
25
    {
26 7
        $mediaType = $file->getClientMediaType() ?? throw new RuleException('Media Type ins null');
27
28 6
        if ($this->allowedAllMediaType) {
29 1
            return;
30
        }
31
32 5
        list($type, $subType) = $this->explode($mediaType);
33
34 5
        if (!array_key_exists($type, $this->allowedMediaType)){
35 1
            throw new RuleException(sprintf($this->errorMessage, sprintf('%s/*', $type)));
36
        }
37
38
        /** @var string|string[] $allowed */
39 4
        $allowed = $this->allowedMediaType[$type];
40
41 4
        if ($allowed === '*') {
42 1
            return;
43
        }
44
45
        /** @var string[] $allowed */
46 3
        if (!in_array($subType, $allowed, true)) {
47 2
            throw new RuleException(sprintf($this->errorMessage, $mediaType));
48
        }
49
    }
50
51 15
    public function allow(string $string): MediaType
52
    {
53 15
        if (!str_contains($string, '/')) {
54 2
            throw new RuleException(sprintf('Media Type is wrong: %s', $string));
55
        }
56
57 13
        list($type, $subType) = $this->explode($string);
58
59 7
        if ($type === '*') {
60 2
            $this->allowedAllMediaType = true;
61 2
            return $this;
62
        }
63
64
        /** @var string[]|string $allowType */
65 6
        $allowType = $this->allowedMediaType[$type] ?? [];
66
67
68 6
        if ($allowType === '*') {
69 1
            return $this;
70
        }
71
72 6
        if ($subType === '*') {
73 2
            $allowType = $subType;
74
        } else {
75 5
            $allowType[] = $subType;
76
        }
77
78 6
        if (is_array($allowType)) {
79 5
            $allowType = array_unique($allowType);
80
        }
81
82 6
        $this->allowedMediaType[$type] = $allowType;
83 6
        return $this;
84
    }
85
86
87 1
    public function getAllowedMediaType(): array|string
88
    {
89 1
        return $this->allowedMediaType;
90
    }
91
92
    /**
93
     * @param string $string
94
     * @return string[]
95
     */
96 13
    private function explode(string $string): array
97
    {
98 13
        list($type, $subType) = explode('/', trim($string));
99
100 13
        if (empty($type)
101 11
            || empty($subType)
102 9
            || str_ends_with($type, ' ')
103 13
            || str_starts_with($subType, ' ')
104
        ) {
105 6
            throw new RuleException(sprintf('Media Type is wrong: `%s`', $string));
106
        }
107
108 7
        return array($type, $subType);
109
    }
110
}
111