Request::exec()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace seregazhuk\SmsIntel\Api\Requests;
4
5
use GuzzleHttp\Client;
6
use seregazhuk\SmsIntel\Api\GuzzleHttpClient;
7
8
abstract class Request
9
{
10
11
    /**
12
     * @var Client
13
     */
14
    protected $client;
15
16
    /**
17
     * @var string
18
     */
19
    protected $login;
20
21
    /**
22
     * @var string
23
     */
24
    protected $password;
25
26
    public function __construct(GuzzleHttpClient $http)
27
    {
28
        $this->client = $http;
0 ignored issues
show
Documentation Bug introduced by
It seems like $http of type object<seregazhuk\SmsIntel\Api\GuzzleHttpClient> is incompatible with the declared type object<GuzzleHttp\Client> of property $client.

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...
29
    }
30
31
32
    /**
33
     * @param string $login
34
     * @param string $password
35
     * @return $this
36
     */
37
    public function setCredentials($login, $password)
38
    {
39
        $this->login = $login;
40
        $this->password = $password;
41
42
        return $this;
43
    }
44
45
    /**
46
     * Make the request to API
47
     *
48
     * @param string $action
49
     * @param array $params
50
     * @return array|null
51
     */
52
    public function exec($action, $params = [])
53
    {
54
        $endPoint = $this->makeEndPoint($action);
55
        $requestBody = $this->createRequestBody($params);
56
57
        $response = $this->client->post($endPoint, $requestBody);
0 ignored issues
show
Documentation introduced by
$requestBody is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
        return $this->parseResponse($response);
0 ignored issues
show
Documentation introduced by
$response is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
    }
60
61
    /**
62
     * @param array $params
63
     * @return string
64
     */
65
    protected function createRequestBody(array $params)
66
    {
67
        $params = array_merge(
68
            [
69
                'login'    => $this->login,
70
                'password' => $this->password,
71
            ],
72
            $params
73
        );
74
75
        return $this->formatRequestBody($params);
76
    }
77
78
    /**
79
     * @param array $requestBody
80
     * @return mixed
81
     */
82
    abstract protected function formatRequestBody(array $requestBody);
83
84
    /**
85
     * @param string $response
86
     * @return array
87
     */
88
    abstract protected function parseResponse($response);
89
90
    /**
91
     * @param string $action
92
     * @return string
93
     */
94
    abstract protected function makeEndPoint($action);
95
}
96