Completed
Push — master ( afbcb8...9e80c6 )
by Dmitry
01:52
created

Service::setUseOperatorUnits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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