Completed
Pull Request — master (#5)
by Dmitry
05:48
created

Service::handleErrorResponse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 2
nop 1
crap 12
1
<?php
2
/**
3
 * @author Dmitry Gladyshev <[email protected]>
4
 * @date 17/08/2016 11:28
5
 */
6
7
namespace Yandex\Direct;
8
9
use Yandex\Direct\Exception\ErrorResponseException;
10
use Yandex\Direct\Transport\Request;
11
use Yandex\Direct\Transport\TransportInterface;
12
13
/**
14
 * Class Service
15
 * @package Yandex\Direct
16
 */
17
abstract class Service implements ConfigurableInterface
18
{
19
    use ConfigurableTrait;
20
21
    /**
22
     * @var string
23
     */
24
    protected $name;
25
26
    /**
27
     * @var TransportInterface
28
     */
29
    protected $transport;
30
31
    /**
32
     * @var CredentialsInterface
33
     */
34
    protected $credentials;
35
36
    /**
37
     * @var bool
38
     */
39
    private $useOperatorUnits = true;
40
41
    /**
42
     * @param string $name
43
     */
44 49
    public function setName($name)
45
    {
46 49
        $this->name = $name;
47 49
    }
48
49
    /**
50
     * @param TransportInterface $transport
51
     */
52 49
    public function setTransport(TransportInterface $transport)
53
    {
54 49
        $this->transport = $transport;
55 49
    }
56
57
    /**
58
     * @param CredentialsInterface $credentials
59
     */
60 49
    public function setCredentials(CredentialsInterface $credentials)
61
    {
62 49
        $this->credentials = $credentials;
63 49
    }
64
65
    /**
66
     * @param bool $useOperatorUnits
67
     */
68
    public function setUseOperatorUnits($useOperatorUnits)
69
    {
70
        $this->useOperatorUnits = $useOperatorUnits;
71
    }
72
73
    /**
74
     * Do API request with needle headers
75
     *
76
     * @param array $params
77
     * @param array $headers
78
     * @return array
79
     * @throws ErrorResponseException
80
     */
81
    public function request(array $params, $headers = [])
82
    {
83
        /* Request API */
84
85
        $request = new Request([
86
            'service' => $this->name,
87
            'credentials' => $this->credentials,
88
            'params' => $params,
89
            'headers' => $headers,
90
            'useOperatorUnits' => $this->useOperatorUnits
91
        ]);
92
        $response = $this->transport->request($request);
93
        $result = json_decode($response->getBody(), true);
94
95
96
        /* Handle error response */
97
98
        if (isset($result['error'])
99
            && $result['error']
100
        ) {
101
            throw new ErrorResponseException(
102
                $result['error']['error_string'],
103
                $result['error']['error_detail'],
104
                $result['error']['error_code'],
105
                $result['error']['request_id']
106
            );
107
        }
108
109
110
        /* Prepare results */
111
112
        $result['units'] = [
113
            'debit' => $response->getUnitsDebit(),
114
            'limit' => $response->getUnitsLimit(),
115
            'rest' => $response->getUnitsRest()
116
        ];
117
        $result['request_id'] = $response->getRequestId();
118
119
        return $result;
120
    }
121
}
122