ContentTypeMatcher   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 39
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B isContentTypeSupported() 0 26 8
A removeDirectives() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Paysera\Bundle\ApiBundle\Service;
5
6
/**
7
 * @internal
8
 */
9
class ContentTypeMatcher
10
{
11
    /**
12
     * @param string $fullContentType
13
     * @param array $supportedContentTypes Can be `something/something`, `something/*` or `*` to allow all
14
     * @return bool
15
     */
16 45
    public function isContentTypeSupported(string $fullContentType, array $supportedContentTypes): bool
17
    {
18 45
        $contentType = $this->removeDirectives($fullContentType);
19 45
        foreach ($supportedContentTypes as $availableContentType) {
20 45
            if ($contentType === $availableContentType) {
21 28
                return true;
22
            }
23
24 38
            if ($availableContentType === '*') {
25 2
                return true;
26
            }
27
28 36
            $availableParts = explode('/', $availableContentType, 2);
29 36
            $providedParts = explode('/', $contentType, 2);
30
31 36
            if (count($availableParts) < 2 || count($providedParts) < 2) {
32 21
                continue;
33
            }
34
35 16
            if ($availableParts[0] === $providedParts[0] && $availableParts[1] === '*') {
36 16
                return true;
37
            }
38
        }
39
40 11
        return false;
41
    }
42
43 45
    private function removeDirectives(string $fullContentType): string
44
    {
45 45
        return explode(';', $fullContentType, 2)[0];
46
    }
47
}
48