Passed
Pull Request — master (#5)
by Ayan
11:10
created

Mime::processMime()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5.0042

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 21
c 3
b 0
f 0
dl 0
loc 30
ccs 17
cts 18
cp 0.9444
rs 9.2728
cc 5
nc 5
nop 1
crap 5.0042
1
<?php
2
3
namespace Phprest\Util;
4
5
use League\Container\Container;
6
use Phprest\Application;
7
use Phprest\Util\DataStructure\MimeProcessResult;
8
9
trait Mime
10
{
11
12 17
    protected function processMime(string $mime): MimeProcessResult
13
    {
14 17
        $vendor = (string)$this->getContainer()->get(Application::CONTAINER_ID_VENDOR);
15 17
        $apiVersion = (string)$this->getContainer()->get(Application::CONTAINER_ID_API_VERSION);
16 17
        $apiVersionRegExp = Application::API_VERSION_REG_EXP;
17 17
        $format = null;
18
19 17
        if (preg_match(
0 ignored issues
show
Coding Style introduced by
The first expression of a multi-line control structure must be on the line after the opening parenthesis
Loading history...
20 17
            '#application/vnd\.' . $vendor . '-v' . $apiVersionRegExp . '\+(xml|json)#',
21
            $mime,
22
            $matches
23
        )
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line control structure must be indented at least once; expected at least 12 spaces, but found 8
Loading history...
24
        ) {
25 1
            [$mime, $apiVersion, $format] = $matches;
26
        } elseif (preg_match(
0 ignored issues
show
Coding Style introduced by
The first expression of a multi-line control structure must be on the line after the opening parenthesis
Loading history...
27 16
            '#application/vnd\.' . $vendor . '\+(xml|json).*?version=' . $apiVersionRegExp . '#',
28
            $mime,
29
            $matches
30
        )
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line control structure must be indented at least once; expected at least 12 spaces, but found 8
Loading history...
31
        ) {
32 1
            [$mime, $format, $apiVersion] = $matches;
33 15
        } elseif ('application/json' === $mime) {
34 3
            $format = 'json';
35 3
            $mime = 'application/vnd.' . $vendor . '-v' . $apiVersion . '+json';
36 12
        } elseif ('application/xml' === $mime) {
37 2
            $format = 'xml';
38 2
            $mime = 'application/vnd.' . $vendor . '-v' . $apiVersion . '+xml';
39
        }
40
41 17
        return new MimeProcessResult($mime, $vendor, $apiVersion, $format);
42
    }
43
44
    abstract protected function getContainer();
45
}
46