MimeTypesAllowed   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 54
ccs 14
cts 14
cp 1
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getExtensions() 0 4 1
A addMap() 0 7 3
A __construct() 0 7 2
A getMimeTypes() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Devanych\Mime;
6
7
use InvalidArgumentException;
8
use LogicException;
9
10
use function array_unique;
11
use function strtolower;
12
use function trim;
13
14
final class MimeTypesAllowed implements MimeTypesInterface
15
{
16
    use MimeTypesTrait;
17
18
    /**
19
     * @var array<string, string[]>
20
     */
21
    private array $extensions = [];
22
23
    /**
24
     * @var array<string, string[]>
25
     */
26
    private array $mimeTypes = [];
27
28
    /**
29
     * @param array<string, string[]> $map
30
     */
31 13
    public function __construct(array $map)
32
    {
33 13
        if (empty($map)) {
34 1
            throw new InvalidArgumentException('Map with allowed mime types cannot be empty');
35
        }
36
37 12
        $this->addMap($map);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 1
    public function getExtensions(string $mimeType): array
44
    {
45 1
        $lowerMime = strtolower(trim($mimeType));
46 1
        return array_unique($this->extensions[$lowerMime] ?? $this->extensions[$mimeType] ?? []);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1
    public function getMimeTypes(string $extension): array
53
    {
54 1
        $lowerExt = strtolower(trim($extension));
55 1
        return array_unique($this->mimeTypes[$lowerExt] ?? $this->mimeTypes[$extension] ?? []);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 12
    public function addMap(array $map): void
62
    {
63 12
        if ($this->extensions !== [] || $this->mimeTypes !== []) {
64 1
            throw new LogicException('Map with allowed mime types already added');
65
        }
66
67 12
        $this->addMapInternal($map);
68
    }
69
}
70