Passed
Push — main ( 383cbf...40aa9a )
by Dylan
02:01
created

ApiService::retrieve()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 2
nop 2
1
<?php
2
3
namespace Lifeboat\Services;
4
5
use Lifeboat\Connector;
6
use Lifeboat\Exceptions\ApiException;
7
use Lifeboat\Exceptions\OAuthException;
8
use Lifeboat\Factory\ObjectFactory;
9
use Lifeboat\Models\Model;
10
11
/**
12
 * Class ApiService
13
 * @package Lifeboat\Services
14
 *
15
 * @property Connector $client
16
 */
17
abstract class ApiService {
18
19
    /** @var Connector $client */
20
    protected Connector $client;
21
22
    /**
23
     * @param int $id
24
     * @return Model|null
25
     */
26
    abstract public function fetch(int $id = -1): ?Model;
27
28
    public function __construct(Connector $client)
29
    {
30
        $this->setClient($client);
31
    }
32
33
    /**
34
     * @return Connector
35
     */
36
    public function getClient(): Connector
37
    {
38
        return $this->client;
39
    }
40
41
    /**
42
     * @param Connector $client
43
     * @return $this
44
     */
45
    public function setClient(Connector $client): ApiService
46
    {
47
        $this->client = $client;
48
        return $this;
49
    }
50
51
    /**
52
     * @see ObjectFactory::make()
53
     * @see Connector::curl_api()
54
     *
55
     * @param string $url
56
     * @param array $params
57
     * @return Model|null
58
     * @throws ApiException
59
     * @throws OAuthException
60
     */
61
    protected function retrieve(string $url, array $params = []): ?Model
62
    {
63
        $curl = $this->getClient()->curl_api($url, 'GET', $params);
64
65
        if ($curl->isValid() && $curl->isJSON()) {
66
            return ObjectFactory::make($this->getClient(), $curl->getJSON());
0 ignored issues
show
Bug Best Practice introduced by
The expression return Lifeboat\Factory\...nt(), $curl->getJSON()) returns the type Lifeboat\Resource\ObjectResource which includes types incompatible with the type-hinted return Lifeboat\Models\Model|null.
Loading history...
67
        }
68
69
        throw new ApiException($curl->getError());
70
    }
71
}
72