Completed
Push — master ( 9a342a...a162ae )
by Andrii
13:06
created

ServerRequestFactory::parseGlobals()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.9617
c 0
b 0
f 0
cc 6
nc 16
nop 0
1
<?php
2
3
namespace hiapi\Core\Console;
4
5
use Laminas\Diactoros\ServerRequest;
6
use Laminas\Diactoros\Uri;
7
8
/**
9
 * Class for marshaling a request object sent with console.
10
 */
11
class ServerRequestFactory
12
{
13
    const PROTOCOL_VERSION = '1.1';
14
15
    private $program;
16
17
    private $command;
18
19
    private $ops = [];
20
21
    private $query = [];
22
23
    private $post = [];
24
25
    /**
26
     * Create a request from superglobal values: argv.
27
     * @return ServerRequest
28
     */
29
    public static function fromGlobals(): ServerRequest
30
    {
31
        return (new static())->createFromGlobals();
32
    }
33
34
    public function createFromGlobals(): ServerRequest
35
    {
36
        $this->parseGlobals();
37
38
        return $this->addAttributes(new ServerRequest(
39
            $_SERVER,
40
            $_FILES,
41
            $this->prepareUri($this->command),
42
            'POST',
43
            'php://input',
44
            $this->prepareHeaders($this->ops),
45
            $this->prepareCookies(),
46
            $this->query,
47
            $this->post,
48
            static::PROTOCOL_VERSION
49
        ), $this->ops);
50
    }
51
52
    private function parseGlobals()
53
    {
54
        $argv = $GLOBALS['argv'];
55
        $this->program = array_shift($argv);
56
        while ($argv[0][0]=='-') {
57
            $vs = explode('=', substr(array_shift($argv), 1), 2);
58
            $this->ops[$vs[0]] = $vs[1] ?? true;
59
        }
60
        $this->command = array_shift($argv);
61
62
        foreach ($argv as $n => $arg) {
63
            if ($arg[0] == '-') {
64
                $vs = explode('=', substr($arg, 1), 2);
65
                $this->query[$vs[0]] = is_null($vs[1]) ? true : $vs[1];
66
            } else $this->query[$n] = $arg;
67
        }
68
69
        if (!empty($this->ops['stdin'])) {
70
            $this->post = json_decode(file_get_contents('php://stdin'), true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode(file_get_con...s('php://stdin'), true) of type * is incompatible with the declared type array of property $post.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
        }
72
    }
73
74
    private function addAttributes(ServerRequest $request, array $ops): ServerRequest
75
    {
76
        foreach ($ops as $key => $value) {
77
            $request = $request->withAttribute($key, $value);
78
        }
79
80
        return $request;
81
    }
82
83
    private function prepareUri(string $command): Uri
84
    {
85
        $uri = new Uri('');
86
87
        return $uri->withPath("/$command");
88
    }
89
90
    private function prepareHeaders(array $ops): array
91
    {
92
        return [
93
            'Accept' => $this->detectContentType($ops),
94
        ];
95
    }
96
97
    private function detectContentType(array $ops): string
98
    {
99
        if (!empty($ops['text'])) {
100
            return 'text/plain';
101
        }
102
        if (!empty($ops['show']) || !empty($ops['dump'])) {
103
            return 'text/php';
104
        }
105
106
        return 'application/json';
107
    }
108
109
    private function prepareCookies(): array
110
    {
111
        return $_COOKIE;
112
    }
113
}
114