BaseService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace UsamamuneerChaudhary\Daftravel\Services;
4
5
use UsamamuneerChaudhary\Daftravel\Http\Client;
6
7
abstract class BaseService
8
{
9
    protected Client $client;
10
    protected array $config;
11
    protected ?string $endpoint;
12
13
    public function __construct(Client $client, array $config)
14
    {
15
        $this->client = $client;
16
        $this->config = $config;
17
    }
18
19
    public function all(array $params = [])
20
    {
21
        return $this->client->get($this->endpoint . '.json', $params);
22
    }
23
24
    public function find(int $id)
25
    {
26
        return $this->client->get("{$this->endpoint}/{$id}.json");
27
    }
28
29
    public function create(array $data)
30
    {
31
        return $this->client->post($this->endpoint, $data);
0 ignored issues
show
Bug introduced by
It seems like $this->endpoint can also be of type null; however, parameter $endpoint of UsamamuneerChaudhary\Daftravel\Http\Client::post() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
        return $this->client->post(/** @scrutinizer ignore-type */ $this->endpoint, $data);
Loading history...
32
    }
33
34
    public function update(int $id, array $data)
35
    {
36
        return $this->client->put("{$this->endpoint}/{$id}", $data);
37
    }
38
39
    public function delete(int $id)
40
    {
41
        return $this->client->delete("{$this->endpoint}/{$id}");
42
    }
43
44
    public function paginate(int $page = 1, int $limit = 20, array $params = [])
45
    {
46
        // Validate parameters according to Daftra API documentation
47
        $page = max(1, $page); // page >= 1
48
        $limit = max(1, min(1000, $limit)); // limit between 1 and 1000
49
50
        $params['page'] = $page;
51
        $params['limit'] = $limit;
52
53
        return $this->client->get($this->endpoint . '.json', $params);
54
    }
55
}
56