Completed
Pull Request — master (#3)
by Sergey
02:17
created

Request::exec()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
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
    const BASE_URL = 'https://lcab.smsintel.ru/API/XML/';
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
     * @return array
34
     */
35
    public static function getAllowedMethods()
36
    {
37
        return [];
38
    }
39
40
    /**
41
     * @param string $login
42
     * @param string $password
43
     * @return $this
44
     */
45
    public function setCredentials($login, $password)
46
    {
47
        $this->login = $login;
48
        $this->password = $password;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Make the request to API
55
     *
56
     * @param string $action
57
     * @param array $params
58
     * @return array|null
59
     */
60
    public function exec($action, $params = [ ])
61
    {
62
        $endPoint = $this->makeEndPoint($action);
63
        $requestBody = $this->createRequestBody($params);
64
65
        $response = $this->client->post($endPoint, $requestBody);
66
        return $this->parseResponse($response);
67
    }
68
69
    /**
70
     * @param string $action
71
     * @return string
72
     */
73
    abstract protected function makeEndPoint($action);
74
75
    /**
76
     * @param array $params
77
     * @return string
78
     */
79
    protected function createRequestBody(array $params)
80
    {
81
        $params = array_merge(
82
            [
83
                'login'    => $this->login,
84
                'password' => $this->password,
85
            ],
86
            $params);
87
88
        return $this->formatRequestBody($params);
89
    }
90
91
    /**
92
     * @param array $requestBody
93
     * @return mixed
94
     */
95
    abstract protected function formatRequestBody(array $requestBody);
96
97
    abstract protected function parseResponse($response);
98
}