ApiAbstract   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 4
Metric Value
wmc 5
c 5
b 0
f 4
lcom 1
cbo 1
dl 0
loc 71
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A post() 0 8 1
A postRaw() 0 8 1
A createJsonBody() 0 8 2
1
<?php
2
3
namespace Artstorm\MonkeyLearn\Api;
4
5
use Artstorm\MonkeyLearn\HttpClient\Response;
6
use Artstorm\MonkeyLearn\Client;
7
8
abstract class ApiAbstract
9
{
10
    /**
11
     * HTTP client.
12
     *
13
     * @var Client
14
     */
15
    protected $client;
16
17
    /**
18
     * Assign dependencies.
19
     *
20
     * @param  Client $client
21
     */
22 12
    public function __construct(Client $client)
23
    {
24 12
        $this->client = $client;
25 12
    }
26
27
    /**
28
     * Send a POST request with JSON encoded parameters.
29
     *
30
     * @param  string $path
31
     * @param  array  $parameters
32
     * @param  array  $headers
33
     *
34
     * @return Response
35
     */
36 6
    protected function post($path, array $parameters = [], array $headers = [])
37
    {
38 6
        return $this->postRaw(
39 6
            $path,
40 6
            $this->createJsonBody($parameters),
41
            $headers
42 6
        );
43
    }
44
45
    /**
46
     * Send a POST request with raw data.
47
     *
48
     * @param  string $path
49
     * @param  mixed  $body
50
     * @param  array  $headers
51
     *
52
     * @return Response
53
     */
54 6
    protected function postRaw($path, $body, array $headers = [])
55
    {
56 6
        return $this->client->getHttpClient()->post(
57 6
            $path,
58 6
            $body,
59
            $headers
60 6
        );
61
    }
62
63
    /**
64
     * Create a JSON encoded version of an array of parameters.
65
     *
66
     * @param  array $parameters
67
     *
68
     * @return null|string
69
     */
70 6
    protected function createJsonBody(array $parameters)
71
    {
72 6
        if (count($parameters) === 0) {
73 2
            return;
74
        }
75
76 4
        return json_encode($parameters);
77
    }
78
}
79