Completed
Push — master ( 08a082...3e013b )
by Juliano
08:44 queued 06:59
created

Endpoint::page()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 4
nop 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
    public function __construct(Client $client)
29
    {
30
        $this->client = $client;
31
    }
32
33
    /**
34
     * Set the client branch id.
35
     *
36
     * @param int $branchId
37
     *
38
     * @return self
39
     */
40
    public function setBranch($branchId)
41
    {
42
        $this->branchId = $branchId;
43
        $this->client->setBranch($branchId);
44
45
        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
    protected function page($point, array $query = [])
57
    {
58
        $query['pageSize'] = isset($query['pageSize']) ? $query['pageSize'] : 100;
59
        $query['start'] = isset($query['start']) ? $query['start'] : 0;
60
61
        return $this->run('GET', $point, ['query' => $query]);
62
    }
63
64
    /**
65
     * Run the endpoint operation.
66
     *
67
     * @return stdObject
68
     */
69
    protected function run($method, $endpoint, array $data = [])
70
    {
71
        if (!$this->branchId) {
72
            $this->setBranch(1);
73
        }
74
75
        $request = new Request($this->client->getHandler());
76
77
        return $request->run($method, $this->client->makeUrl($endpoint), array_merge($data, [
78
            'headers' => [
79
                'X-Session-ID' => $this->client->getToken(),
80
            ],
81
        ]));
82
    }
83
}
84