Completed
Push — master ( 6aaa22...14d740 )
by Sergey
03:21 queued 34s
created

Request::formatRequestBody()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
nc 1
1
<?php
2
3
namespace seregazhuk\SmsIntel\Requests;
4
5
use seregazhuk\SmsIntel\Contracts\HttpInterface;
6
use seregazhuk\SmsIntel\Contracts\RequestInterface;
7
8
abstract class Request implements RequestInterface
9
{
10
    static public $allowedMethod = [];
11
12
    /**
13
     * @var HttpInterface
14
     */
15
    protected $client;
16
17
    /**
18
     * @var string
19
     */
20
    protected $login;
21
22
    /**
23
     * @var string
24
     */
25
    protected $password;
26
27
    public function __construct(HttpInterface $http)
28
    {
29
        $this->client = $http;
30
    }
31
32
33
    /**
34
     * @param string $login
35
     * @param string $password
36
     * @return $this
37
     */
38
    public function setCredentials($login, $password)
39
    {
40
        $this->login = $login;
41
        $this->password = $password;
42
43
        return $this;
44
    }
45
46
    /**
47
     * Make the request to API
48
     *
49
     * @param string $action
50
     * @param array $params
51
     * @return array|null
52
     */
53
    public function exec($action, $params = [])
54
    {
55
        $endPoint = $this->makeEndPoint($action);
56
        $requestBody = $this->createRequestBody($params);
57
58
        $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...
59
        return $this->parseResponse($response);
0 ignored issues
show
Documentation introduced by
$response is of type array, 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...
60
    }
61
62
    /**
63
     * @param array $params
64
     * @return string
65
     */
66
    protected function createRequestBody(array $params)
67
    {
68
        $params = array_merge(
69
            [
70
                'login'    => $this->login,
71
                'password' => $this->password,
72
            ],
73
            $params
74
        );
75
76
        return $this->formatRequestBody($params);
77
    }
78
79
    /**
80
     * @param array $requestBody
81
     * @return mixed
82
     */
83
    abstract protected function formatRequestBody(array $requestBody);
84
85
    /**
86
     * @param string $response
87
     * @return array
88
     */
89
    abstract protected function parseResponse($response);
90
91
    /**
92
     * @param string $action
93
     * @return string
94
     */
95
    abstract public function makeEndPoint($action);
96
}