Passed
Pull Request — master (#255)
by Christopher
04:39 queued 01:18
created

IncomingChangeSetRequest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 27
c 2
b 0
f 0
dl 0
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 33 7
A getContentId() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace POData\BatchProcessor;
6
7
use POData\OperationContext\HTTPRequestMethod;
8
use POData\OperationContext\Web\IncomingRequest;
9
10
class IncomingChangeSetRequest extends IncomingRequest
11
{
12
    protected $contentID = null;
13
14
    public function __construct(string $requestChunk)
15
    {
16
        list($RequestParams, $requestHeaders, $RequestBody) = explode("\n\n", $requestChunk);
17
18
        $headerLine                                         = strtok($requestHeaders, "\n");
19
        $RequestBody                                        = trim($RequestBody);
20
        list($RequesetType, $RequestPath, $RequestProticol) = explode(' ', $headerLine, 3);
21
        $inboundRequestHeaders                              = [];
22
        $headerLine                                         = strtok("\n");
23
24
        while ($headerLine !== false) {
25
            list($key, $value)            = explode(':', $headerLine);
26
            $name                         = strtr(strtoupper(trim($key)), '-', '_');
27
            $value                        = trim($value);
28
            $name                         = substr($name, 0, 5) === 'HTTP_' || $name == 'CONTENT_TYPE' ? $name : 'HTTP_' . $name;
29
            if ('HTTP_CONTENT_ID' === $name) {
30
                $this->contentID = $value;
31
            } else {
32
                $inboundRequestHeaders[$name] = $value;
33
            }
34
            $headerLine = strtok("\n");
35
        }
36
        $host     = $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? $_SERVER['SERVER_ADDR'] ?? 'localhost';
37
        $protocol = $_SERVER['PROTOCOL'] = isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ? 'https' : 'http';
38
39
        parent::__construct(
40
            new HTTPRequestMethod($RequesetType),
41
            [],
42
            [],
43
            $inboundRequestHeaders,
44
            null,
45
            $RequestBody,
46
            $protocol . '://' . $host . $RequestPath
47
        );
48
    }
49
50
    public function getContentId(): ?string
51
    {
52
        return $this->contentID;
53
    }
54
}
55