Completed
Push — master ( a46561...dc2809 )
by Dmitry
02:55
created

Service   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 24.32%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 97
rs 10
c 0
b 0
f 0
ccs 9
cts 37
cp 0.2432

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setName() 0 4 1
A setTransport() 0 4 1
A setCredentials() 0 4 1
A handleErrorResponse() 0 15 3
B request() 0 24 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\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 Credentials
35
     */
36
    protected $credentials;
37
38
    /**
39
     * @param string $name
40
     */
41 30
    public function setName($name)
42
    {
43 30
        $this->name = $name;
44 30
    }
45
46
    /**
47
     * @param TransportInterface $transport
48
     */
49 30
    public function setTransport($transport)
50
    {
51 30
        $this->transport = $transport;
52 30
    }
53
54
    /**
55
     * @param Credentials $credentials
56
     */
57 30
    public function setCredentials($credentials)
58
    {
59 30
        $this->credentials = $credentials;
60 30
    }
61
62
    /**
63
     * @param TransportResponse $response
64
     * @throws ErrorResponseException
65
     */
66
    protected static function handleErrorResponse(TransportResponse $response)
67
    {
68
        $json = json_decode($response->getBody(), true);
69
70
        if (isset($json['error'])
71
            && $json['error']
72
        ) {
73
            throw new ErrorResponseException(
74
                $json['error']['error_string'],
75
                $json['error']['error_detail'],
76
                $json['error']['error_code'],
77
                $json['error']['request_id']
78
            );
79
        }
80
    }
81
82
    /**
83
     * Do API request with needle headers
84
     *
85
     * @param array $params
86
     * @param bool $useOperatorUnits
87
     * @param array $headers
88
     * @return mixed
89
     */
90
    public function request(array $params, $useOperatorUnits = true, $headers = [])
91
    {
92
        $response = $this->transport->request(new TransportRequest([
93
            'service' => $this->name,
94
            'credentials' => $this->credentials,
95
            'params' => $params,
96
            'headers' => $headers,
97
            'useOperatorUnits' => $useOperatorUnits
98
        ]));
99
100
        self::handleErrorResponse($response);
101
102
        $result = json_decode($response->getBody(), true);
103
104
        $result['units'] = [
105
            'debit' => $response->getUnitsDebit(),
106
            'limit' => $response->getUnitsLimit(),
107
            'rest' => $response->getUnitsRest()
108
        ];
109
110
        $result['request_id'] = $response->getRequestId();
111
112
        return $result;
113
    }
114
115
}