Completed
Push — master ( 0f43cb...7121ae )
by Alex
18s queued 14s
created

IncomingChangeSetRequest::setupHeaders()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 16
rs 9.5555
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
        list($RequesetType, $RequestPath, $RequestProticol) = explode(' ', $headerLine, 3);
20
21
        $inboundRequestHeaders = $this->setupHeaders(strtok("\n"));
22
23
        $RequestBody                                        = trim($RequestBody);
24
25
        $host     = $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? $_SERVER['SERVER_ADDR'] ?? 'localhost';
26
        $protocol = $_SERVER['PROTOCOL'] = isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ? 'https' : 'http';
27
28
        parent::__construct(
29
            new HTTPRequestMethod($RequesetType),
30
            [],
31
            [],
32
            $inboundRequestHeaders,
33
            null,
34
            $RequestBody,
35
            $protocol . '://' . $host . $RequestPath
36
        );
37
    }
38
39
    public function getContentId(): ?string
40
    {
41
        return $this->contentID;
42
    }
43
44
    /**
45
     * @param string $headerLine
46
     * @return array
47
     */
48
    protected function setupHeaders(string $headerLine): array
49
    {
50
        $inboundRequestHeaders = [];
51
        while ($headerLine !== false) {
52
            list($key, $value) = explode(':', $headerLine);
53
            $name = strtr(strtoupper(trim($key)), '-', '_');
54
            $value = trim($value);
55
            $name = substr($name, 0, 5) === 'HTTP_' || $name == 'CONTENT_TYPE' ? $name : 'HTTP_' . $name;
56
            if ('HTTP_CONTENT_ID' === $name) {
57
                $this->contentID = $value;
58
            } else {
59
                $inboundRequestHeaders[$name] = $value;
60
            }
61
            $headerLine = strtok("\n");
62
        }
63
        return $inboundRequestHeaders;
64
    }
65
66
67
    /**
68
     * @param string $contentID
69
     */
70
    public function applyContentID(string $contentID, string $contentIdValue): void
71
    {
72
        $this->rawInput    = str_replace('$' . $contentID, $contentIdValue, $this->rawInput);
73
        $this->rawUrl      = str_replace('$' . $contentID, $contentIdValue, $this->rawUrl);
74
    }
75
}
76