Completed
Push — master ( da1cfa...dccc7f )
by Vladimir
03:06
created

Driver::getHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Drivers;
6
7
use FondBot\Helpers\Arr;
8
use FondBot\Queue\SerializableForQueue;
9
use Psr\Http\Message\ServerRequestInterface;
10
use FondBot\Drivers\Exceptions\InvalidRequest;
11
12
abstract class Driver implements SerializableForQueue
13
{
14
    /** @var ServerRequestInterface */
15
    protected $request = [];
16
17
    /** @var array */
18
    protected $parameters;
19
20
    /**
21
     * Set driver data.
22
     *
23
     * @param array $parameters
24
     * @param ServerRequestInterface $request
25
     */
26 3
    public function fill(array $parameters, ServerRequestInterface $request): void
27
    {
28 3
        $this->parameters = $parameters;
29 3
        $this->request = $request;
30 3
    }
31
32
    /**
33
     * Get request value.
34
     *
35
     * @param string|null $key
36
     * @param null        $default
37
     *
38
     * @return mixed
39
     */
40 1
    public function getRequest(string $key = null, $default = null)
41
    {
42 1
        return Arr::get($this->request->getParsedBody(), $key, $default);
0 ignored issues
show
Bug introduced by
It seems like $this->request->getParsedBody() targeting Psr\Http\Message\ServerR...erface::getParsedBody() can also be of type null or object; however, FondBot\Helpers\Arr::get() does only seem to accept object<ArrayAccess>|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
43
    }
44
45
    /**
46
     * If request has key.
47
     *
48
     * @param string $key
49
     *
50
     * @return bool
51
     */
52 1
    public function hasRequest(string $key): bool
53
    {
54 1
        return Arr::has($this->request->getParsedBody(), [$key]);
0 ignored issues
show
Bug introduced by
It seems like $this->request->getParsedBody() targeting Psr\Http\Message\ServerR...erface::getParsedBody() can also be of type null or object; however, FondBot\Helpers\Arr::has() does only seem to accept object<ArrayAccess>|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
55
    }
56
57
    /**
58
     * Get header.
59
     *
60
     * @param string $name
61
     * @param null   $default
62
     *
63
     * @return mixed
64
     */
65 1
    public function getHeader(string $name = null, $default = null)
66
    {
67 1
        return Arr::get($this->request->getHeaders(), $name, $default);
68
    }
69
70
    /**
71
     * Get parameter value.
72
     *
73
     * @param string $name
74
     * @param null   $default
75
     *
76
     * @return mixed
77
     */
78 1
    public function getParameter(string $name, $default = null)
79
    {
80 1
        return Arr::get($this->parameters, $name, $default);
81
    }
82
83
    /**
84
     * Verify incoming request data.
85
     *
86
     * @throws InvalidRequest
87
     */
88
    abstract public function verifyRequest(): void;
89
90
    /**
91
     * Get current chat.
92
     *
93
     * @return Chat
94
     */
95
    abstract public function getChat(): Chat;
96
97
    /**
98
     * Get current user.
99
     *
100
     * @return User
101
     */
102
    abstract public function getUser(): User;
103
104
    /**
105
     * Get message received from sender.
106
     *
107
     * @return ReceivedMessage
108
     */
109
    abstract public function getMessage(): ReceivedMessage;
110
111
    /**
112
     * Handle command.
113
     *
114
     * @param Command $command
115
     */
116
    abstract public function handle(Command $command): void;
117
}
118