Completed
Push — master ( d1b5d6...d751a6 )
by Sebastian
04:20
created

Body   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
isJson() 0 1 ?
B fromSuperGlobals() 0 17 5
1
<?php
2
namespace Kartenmacherei\RestFramework\Request\Body;
3
4
use Kartenmacherei\RestFramework\Response\Content\ContentType;
5
6
abstract class Body
7
{
8
    /**
9
     * @param string $inputStream
10
     * @return Body|JsonBody
11
     * @throws UnsupportedRequestBodyException
12
     */
13
    public static function fromSuperGlobals(string $inputStream = 'php://input'): Body
14
    {
15
        $content = file_get_contents($inputStream);
16
        if (empty($content)) {
17
            return new EmptyBody();
18
        }
19
20
        if (!isset($_SERVER['CONTENT_TYPE']) || empty($_SERVER['CONTENT_TYPE'])) {
21
            return new RawBody($content);
22
        }
23
24
        switch ($_SERVER['CONTENT_TYPE']) {
25
            case ContentType::JSON:
26
                return new JsonBody($content);
27
        }
28
        throw new UnsupportedRequestBodyException();
29
    }
30
31
    /**
32
     * @return bool
33
     */
34
    abstract public function isJson(): bool;
35
}
36