Passed
Push — master ( a85aeb...922d24 )
by Vladimir
08:38
created

AbstractDriver::getShortName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Drivers;
6
7
use RuntimeException;
8
use FondBot\Helpers\Arr;
9
use FondBot\Http\Request;
10
use Http\Client\HttpClient;
11
use FondBot\Contracts\Driver;
12
use Http\Message\RequestFactory;
13
use Illuminate\Support\Collection;
14
use Psr\Http\Message\StreamInterface;
15
use FondBot\Queue\SerializableForQueue;
16
use Psr\Http\Message\ResponseInterface;
17
use FondBot\Http\Request as HttpRequest;
18
use FondBot\Drivers\Exceptions\InvalidRequest;
19
20
abstract class AbstractDriver implements Driver, SerializableForQueue
21
{
22
    /** @var Collection */
23
    protected $parameters;
24
25
    /** @var HttpRequest */
26
    protected $request;
27
28
    /** @var HttpClient */
29
    private $httpClient;
30
31
    /** @var RequestFactory */
32
    private $requestFactory;
33
34
    public function __construct(HttpClient $httpClient, RequestFactory $requestFactory)
35
    {
36
        $this->httpClient = $httpClient;
37
        $this->requestFactory = $requestFactory;
38
    }
39
40
    /**
41
     * Get driver short name.
42
     *
43
     * This name is used as an alias for configuration.
44
     *
45
     * @return string
46
     */
47
    public function getShortName(): string
48
    {
49
        $shortName = explode('\\', get_class($this));
50
51
        return collect($shortName)->last();
52
    }
53
54
    /**
55
     * Initialize gateway with parameters.
56
     *
57
     * @param array   $parameters
58
     *
59
     * @param Request $request
60
     *
61
     * @return Driver|AbstractDriver|static
62
     */
63 1
    public function initialize(array $parameters, Request $request): Driver
64
    {
65 1
        $this->request = $request;
66
67 1
        $array = [];
68
69 1
        foreach ($this->getDefaultParameters() as $key => $value) {
70 1
            $value = Arr::get($parameters, $key, $value);
71
72 1
            Arr::set($array, $key, $value);
73
        }
74
75 1
        $this->parameters = collect($array);
76
77 1
        return $this;
78
    }
79
80
    /**
81
     * Get parameters.
82
     *
83
     * @return Collection
84
     */
85 1
    public function getParameters(): Collection
86
    {
87 1
        return $this->parameters;
88
    }
89
90
    /**
91
     * Send HTTP request.
92
     *
93
     * @param string                                     $method
94
     * @param string                                     $uri
95
     * @param array                                      $headers
96
     * @param string|array|resource|StreamInterface|null $body
97
     * @param string                                     $protocolVersion
98
     *
99
     * @return ResponseInterface
100
     */
101
    public function send(
102
        string $method,
103
        string $uri,
104
        array $headers = [],
105
        $body = null,
106
        string $protocolVersion = '1.1'
107
    ): ResponseInterface {
108
        $request = $this->requestFactory->createRequest($method, $uri, $headers, $body, $protocolVersion);
109
110
        return $this->httpClient->sendRequest($request);
111
    }
112
113
    /**
114
     * Send a GET request.
115
     *
116
     * @param string $uri
117
     * @param array  $headers
118
     *
119
     * @return ResponseInterface
120
     */
121
    public function get(string $uri, array $headers = []): ResponseInterface
122
    {
123
        return $this->send('GET', $uri, $headers);
124
    }
125
126
    /**
127
     * Send a POST request.
128
     *
129
     * @param string                                     $uri
130
     * @param array                                      $headers
131
     * @param string|array|resource|StreamInterface|null $body
132
     *
133
     * @return ResponseInterface
134
     */
135
    public function post(string $uri, array $headers = [], $body = null): ResponseInterface
136
    {
137
        return $this->send('POST', $uri, $headers, $body);
138
    }
139
140
    /**
141
     * Get template compiler instance.
142
     *
143
     * @return TemplateCompiler|null
144
     */
145
    abstract public function getTemplateCompiler(): ?TemplateCompiler;
146
147
    /**
148
     * Get command handler instance.
149
     *
150
     * @return CommandHandler
151
     */
152
    abstract public function getCommandHandler(): CommandHandler;
153
154
    /**
155
     * Verify incoming request data.
156
     *
157
     * @throws InvalidRequest
158
     */
159
    abstract public function verifyRequest(): void;
160
161
    /**
162
     * Get current chat.
163
     *
164
     * @return Chat
165
     */
166
    abstract public function getChat(): Chat;
167
168
    /**
169
     * Get current user.
170
     *
171
     * @return User
172
     */
173
    abstract public function getUser(): User;
174
175
    /**
176
     * Get message received from sender.
177
     *
178
     * @return ReceivedMessage
179
     */
180
    abstract public function getMessage(): ReceivedMessage;
181
182
    /**
183
     * Handle command.
184
     *
185
     * @param Command $command
186
     *
187
     * @throws RuntimeException
188
     */
189 1
    public function handle(Command $command): void
190
    {
191 1
        $this->getCommandHandler()->handle($command);
192 1
    }
193
}
194