Completed
Push — master ( 37427d...780143 )
by Dmitry
02:21
created

Service::setUseOperatorUnits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 50
    public function setTransport(TransportInterface $transport)
53
    {
54 50
        $this->transport = $transport;
55 50
    }
56
57
    /**
58
     * @param CredentialsInterface $credentials
59
     */
60 50
    public function setCredentials(CredentialsInterface $credentials)
61
    {
62 50
        $this->credentials = $credentials;
63 50
    }
64
65
    /**
66
     * @param bool $useOperatorUnits
67
     */
68 1
    public function setUseOperatorUnits($useOperatorUnits)
69
    {
70 1
        $this->useOperatorUnits = $useOperatorUnits;
71 1
    }
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 1
    public function request(array $params, $headers = [])
82
    {
83
        /* Request API */
84
85 1
        $request = new Request([
86 1
            'service' => $this->name,
87 1
            'credentials' => $this->credentials,
88 1
            'params' => $params,
89 1
            'headers' => $headers,
90 1
            'useOperatorUnits' => $this->useOperatorUnits
91 1
        ]);
92 1
        $response = $this->transport->request($request);
93 1
        $result = json_decode($response->getBody(), true);
94
95
96
        /* Handle error response */
97
98 1
        if (isset($result['error'])
99 1
            && $result['error']
100 1
        ) {
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 1
        $result['units'] = [
113 1
            'debit' => $response->getUnitsDebit(),
114 1
            'limit' => $response->getUnitsLimit(),
115 1
            'rest' => $response->getUnitsRest()
116 1
        ];
117 1
        $result['request_id'] = $response->getRequestId();
118
119 1
        return $result;
120
    }
121
}
122