Endpoint::run()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
crap 2
1
<?php
2
3
namespace JulianoBailao\DomusApi\Core;
4
5
use JulianoBailao\DomusApi\Client;
6
7
abstract class Endpoint
8
{
9
    /**
10
     * The Client object.
11
     *
12
     * @var Client
13
     */
14
    protected $client;
15
16
    /**
17
     * The client branch id.
18
     *
19
     * @var int
20
     */
21
    protected $branchId;
22
23
    /**
24
     * Create a new Endpoint instance.
25
     *
26
     * @param Client $client
27
     */
28 129
    public function __construct(Client $client)
29
    {
30 129
        $this->client = $client;
31 129
    }
32
33
    /**
34
     * Set the client branch id.
35
     *
36
     * @param int $branchId
37
     *
38
     * @return self
39
     */
40 126
    public function setBranch($branchId)
41
    {
42 126
        $this->branchId = $branchId;
43 126
        $this->client->setBranch($branchId);
44
45 126
        return $this;
46
    }
47
48
    /**
49
     * Execute a page request.
50
     *
51
     * @param string $point
52
     * @param array  $query
53
     *
54
     * @return stdClass
55
     */
56 51
    protected function page($point, array $query = [])
57
    {
58 51
        $query['pageSize'] = isset($query['pageSize']) ? $query['pageSize'] : 100;
59 51
        $query['start'] = isset($query['start']) ? $query['start'] : 0;
60
61 51
        return $this->run('GET', $point, ['query' => $query]);
62
    }
63
64
    /**
65
     * Run the endpoint operation.
66
     *
67
     * @return stdObject
68
     */
69 126
    protected function run($method, $endpoint, array $data = [])
70
    {
71 126
        if (!$this->branchId) {
72 126
            $this->setBranch(1);
73 126
        }
74
75 126
        $request = new Request($this->client->getHandler());
76
77 126
        return $request->run($method, $this->client->makeUrl($endpoint), array_merge($data, [
78
            'headers' => [
79 126
                'X-Session-ID' => $this->client->getToken(),
80 126
            ],
81 126
        ]));
82
    }
83
}
84