Completed
Push — master ( f8221d...6a1ba8 )
by Vladimir
09:15
created

Driver::getRequestJson()   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
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Drivers;
6
7
use RuntimeException;
8
use GuzzleHttp\Client;
9
use Illuminate\Http\Request;
10
use FondBot\Channels\Channel;
11
use Illuminate\Support\Collection;
12
use Psr\Http\Message\ResponseInterface;
13
use FondBot\Drivers\Exceptions\InvalidRequest;
14
use FondBot\Contracts\Driver as DriverContract;
15
16
abstract class Driver implements DriverContract
17
{
18
    /** @var Collection */
19
    protected $parameters;
20
21
    /** @var Request */
22
    protected $request;
23
24
    protected $guzzle;
25
26 3
    public function __construct(Client $guzzle)
27
    {
28 3
        $this->guzzle = $guzzle;
29 3
    }
30
31
    /**
32
     * Get driver short name.
33
     *
34
     * This name is used as an alias for configuration.
35
     *
36
     * @return string
37
     */
38 1
    public function getShortName(): string
39
    {
40 1
        $shortName = explode('\\', get_class($this));
41
42 1
        return collect($shortName)->last();
43
    }
44
45
    /**
46
     * Initialize gateway with parameters.
47
     *
48
     * @param Channel $channel
49
     * @param Request $request
50
     *
51
     * @return Driver|DriverContract|static
52
     */
53 2
    public function initialize(Channel $channel, Request $request): DriverContract
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
54
    {
55 2
        $this->request = $request;
56
57 2
        $parameters = [];
58
59 2
        foreach ($this->getDefaultParameters() as $key => $value) {
60 2
            $value = array_get($channel->getParameters(), $key, $value);
61
62 2
            array_set($parameters, $key, $value);
63
        }
64
65 2
        $this->parameters = collect($parameters);
66
67 2
        return $this;
68
    }
69
70
    /**
71
     * Get parameters.
72
     *
73
     * @return Collection
74
     */
75 2
    public function getParameters(): Collection
76
    {
77 2
        return $this->parameters;
78
    }
79
80
    /**
81
     * Get request.
82
     *
83
     * @return Request
84
     */
85
    public function getRequest(): Request
86
    {
87
        return $this->request;
88
    }
89
90
    /**
91
     * Send a GET request.
92
     *
93
     * @param string $uri
94
     * @param array  $options
95
     *
96
     * @return ResponseInterface
97
     */
98 1
    public function get(string $uri, array $options = []): ResponseInterface
99
    {
100 1
        return $this->guzzle->get($uri, $options);
101
    }
102
103
    /**
104
     * Send a POST request.
105
     *
106
     * @param string $uri
107
     * @param array  $options
108
     *
109
     * @return ResponseInterface
110
     */
111 1
    public function post(string $uri, array $options = []): ResponseInterface
112
    {
113 1
        return $this->guzzle->post($uri, $options);
114
    }
115
116
    /**
117
     * Get template compiler instance.
118
     *
119
     * @return TemplateCompiler|null
120
     */
121
    abstract public function getTemplateCompiler(): ?TemplateCompiler;
122
123
    /**
124
     * Get command handler instance.
125
     *
126
     * @return CommandHandler
127
     */
128
    abstract public function getCommandHandler(): CommandHandler;
129
130
    /**
131
     * Verify request consistency.
132
     *
133
     * @throws InvalidRequest
134
     */
135
    abstract public function verifyRequest(): void;
136
137
    /**
138
     * Get current chat.
139
     *
140
     * @return Chat
141
     */
142
    abstract public function getChat(): Chat;
143
144
    /**
145
     * Get current user.
146
     *
147
     * @return User
148
     */
149
    abstract public function getUser(): User;
150
151
    /**
152
     * Get message received from sender.
153
     *
154
     * @return ReceivedMessage
155
     */
156
    abstract public function getMessage(): ReceivedMessage;
157
158
    /**
159
     * Handle command.
160
     *
161
     * @param Command $command
162
     *
163
     * @throws RuntimeException
164
     */
165 1
    public function handle(Command $command): void
166
    {
167 1
        $this->getCommandHandler()->handle($command);
168 1
    }
169
}
170