Tool::parseRepresentationToClass()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 30
rs 8.5806
cc 4
eloc 23
nc 4
nop 1
1
<?php
2
namespace Perry;
3
4
class Tool
5
{
6
7
    /**
8
     * Parse a content-type header to a representation name
9
     *
10
     * @param string $contentType
11
     * @return bool
12
     */
13
    public static function parseContentTypeToRepresentation($contentType)
14
    {
15
        $matches = array();
16
17
        preg_match('/^application\/(.*)\+json; charset=utf-8$/im', $contentType, $matches);
18
19
        if (count($matches) == 2) {
20
            return $matches[1];
21
        }
22
23
        return false;
24
    }
25
26
    /**
27
     * convert a representation name including version to the
28
     * corresponding class
29
     *
30
     * @param  string $inputRepresentation
31
     * @return string
32
     * @throws \Exception
33
     */
34
    public static function parseRepresentationToClass($inputRepresentation)
35
    {
36
        $version = substr($inputRepresentation, -2);
37
        $representation = substr($inputRepresentation, 0, -3);
38
39
        switch (substr($representation, 0, 7)) {
40
            case "vnd.ccp": // EVE
41
                $data = explode(".", $representation);
42
                array_shift($data);
43
                array_shift($data);
44
                array_shift($data);
45
                $classname = '\Perry\Representation\Eve\\'.$version.'\\'.$data[0];
46
                break;
47
            case "net.3rd": // OldApi
48
                $data = explode(".", $representation);
49
                array_shift($data);
50
                array_shift($data);
51
                array_shift($data);
52
53
                $classname = '\Perry\Representation\OldApi\\'.$version.'\\'.$data[0];
54
                if (count($data) > 1) {
55
                    $classname .= '\\'.$data[1];
56
                }
57
                break;
58
            default:
59
                throw new \Exception("wtf, what representation is this? ".$inputRepresentation);
60
        }
61
62
        return $classname;
63
    }
64
}
65