Completed
Push — master ( 21c774...ed97c6 )
by Vladimir
49:18 queued 09:54
created

Request::fromMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Http;
6
7
use FondBot\Helpers\Arr;
8
use Psr\Http\Message\MessageInterface;
9
10
class Request
11
{
12
    private $parameters;
13
    private $headers;
14
15 3
    public function __construct(array $parameters, array $headers)
16
    {
17 3
        $this->parameters = $parameters;
18 3
        $this->headers = $headers;
19 3
    }
20
21 2
    public static function fromMessage(MessageInterface $message): Request
22
    {
23 2
        $parameters = $message->getBody()->getContents();
24
25 2
        if ($parameters === '') {
26 2
            $parameters = '{}';
27
        }
28
29 2
        $parameters = json_decode($parameters, true);
30
31 2
        return new static($parameters, $message->getHeaders());
32
    }
33
34
    /**
35
     * Get request parameters.
36
     *
37
     * @return array
38
     */
39 1
    public function getParameters(): array
40
    {
41 1
        return $this->parameters;
42
    }
43
44
    /**
45
     * Get single parameter.
46
     *
47
     * @param string $key
48
     * @param mixed  $default
49
     *
50
     * @return mixed
51
     */
52 1
    public function getParameter(string $key, $default = null)
53
    {
54 1
        return Arr::get($this->parameters, $key, $default);
55
    }
56
57
    /**
58
     * Determine if request has one or more parameters.
59
     *
60
     * @param array|string $keys
61
     *
62
     * @return bool
63
     */
64 1
    public function hasParameters($keys): bool
65
    {
66 1
        return Arr::has($this->parameters, (array) $keys);
67
    }
68
69
    /**
70
     * Get request headers.
71
     *
72
     * @return array
73
     */
74 1
    public function getHeaders(): array
75
    {
76 1
        return $this->headers;
77
    }
78
79
    /**
80
     * Get single request header.
81
     *
82
     * @param string $key
83
     * @param mixed  $default
84
     *
85
     * @return mixed
86
     */
87 1
    public function getHeader(string $key, $default = null)
88
    {
89 1
        return Arr::get($this->headers, $key, $default);
90
    }
91
}
92