ContentTypeMatcher::isContentTypeSupported()   B
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 14
cts 14
cp 1
rs 8.4444
c 0
b 0
f 0
cc 8
nc 6
nop 2
crap 8
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