Passed
Pull Request — master (#5)
by Ayan
05:27
created

Mime   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
eloc 24
c 3
b 0
f 0
dl 0
loc 43
ccs 17
cts 18
cp 0.9444
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A processMime() 0 32 5
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
        if (
20 17
            preg_match(
21 17
                '#application/vnd\.' . $vendor . '-v' . $apiVersionRegExp . '\+(xml|json)#',
22
                $mime,
23
                $matches
24
            )
25
        ) {
26 1
            [$mime, $apiVersion, $format] = $matches;
27
        } elseif (
28
            preg_match(
29 16
                '#application/vnd\.' . $vendor . '\+(xml|json).*?version=' . $apiVersionRegExp . '#',
30
                $mime,
31
                $matches
32
            )
33
        ) {
34 1
            [$mime, $format, $apiVersion] = $matches;
35 15
        } elseif ('application/json' === $mime) {
36 3
            $format = 'json';
37 3
            $mime   = 'application/vnd.' . $vendor . '-v' . $apiVersion . '+json';
38 12
        } elseif ('application/xml' === $mime) {
39 2
            $format = 'xml';
40 2
            $mime   = 'application/vnd.' . $vendor . '-v' . $apiVersion . '+xml';
41
        }
42
43 17
        return new MimeProcessResult($mime, $vendor, $apiVersion, $format);
44
    }
45
46
    /**
47
     * Returns the DI container.
48
     *
49
     * @return Container
50
     */
51
    abstract protected function getContainer();
52
}
53