Completed
Push — master ( 9db202...d3eb33 )
by Dmitry
02:11
created

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