Completed
Push — master ( bb2732...ede7e6 )
by
unknown
01:49
created

Body   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 6
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
D fromSuperGlobals() 0 25 9
isJson() 0 1 ?
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
17
        if (empty($content) && empty($_POST)) {
18
            return new EmptyBody();
19
        }
20
21
        if (!isset($_SERVER['CONTENT_TYPE']) || empty($_SERVER['CONTENT_TYPE'])) {
22
            return new RawBody($content);
23
        }
24
25
        $contentType = explode(';', $_SERVER['CONTENT_TYPE']);
26
27
        switch (reset($contentType)) {
28
            case ContentType::JSON:
29
            case ContentType::JSON_UTF8:
30
                return new JsonBody($content);
31
            case ContentType::MULTIPART_FORMDATA:
32
                return new FormDataBody($_POST);
33
            case ContentType::PDF:
34
                return new PdfBody($content);
35
        }
36
        throw new UnsupportedRequestBodyException();
37
    }
38
39
    /**
40
     * @return bool
41
     */
42
    abstract public function isJson(): bool;
43
}
44