Passed
Push — main ( dd7998...bae415 )
by Dylan
03:01
created

Connector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 29
c 1
b 0
f 0
dl 0
loc 93
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A auth_url() 0 3 1
A getSites() 0 14 2
A curl_api() 0 11 1
A __get() 0 6 2
A refreshAccessToken() 0 5 1
1
<?php
2
3
namespace Lifeboat;
4
5
use Lifeboat\Exceptions\BadMethodException;
6
use Lifeboat\Exceptions\OAuthException;
7
use Lifeboat\Models\Model;
8
use Lifeboat\Services\ObjectFactory;
9
use Lifeboat\Utils\Curl;
10
11
/**
12
 * Class Connector
13
 * @package Lifeboat
14
 *
15
 * @property string $_auth_domain
16
 * @property string|null $_access_token
17
 * @property string $_site_key
18
 * @property Model $address
19
 */
20
abstract class Connector {
21
22
    const AUTH_DOMAIN   = 'https://accounts.lifeboat.app';
23
    const TOKEN_URL     = '/oauth/token';
24
    const SITES_URL     = '/oauth/sites';
25
26
    protected $_auth_domain     = 'https://accounts.lifeboat.app';
27
    protected $_access_token    = null;
28
    protected $_site_key        = '';
29
    protected $_host            = '';
30
31
    /**
32
     * @return string
33
     *
34
     * @throws OAuthException
35
     */
36
    abstract public function getAccessToken(): string;
37
38
    /**
39
     * @param string $service
40
     * @return Model
41
     * @throws BadMethodException
42
     */
43
    public function __get(string $service): Model
44
    {
45
        $obj = ObjectFactory::create($this, $service);
46
        if (!$obj) throw new BadMethodException("Service for `{$service}` does not exist");
47
48
        return $obj;
49
    }
50
51
    /**
52
     * @return array
53
     * @throws OAuthException
54
     */
55
    public function getSites(): array
56
    {
57
        $curl = new Curl($this->auth_url(self::SITES_URL), [
58
            'access_token' => $this->getAccessToken()
59
        ]);
60
61
        $curl->setMethod('POST');
62
        $response = $curl->curl();
63
64
        if (!$response->isValid()) {
65
            throw new OAuthException($response->getRaw());
66
        }
67
68
        return $response->getJSON();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response->getJSON() could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
69
    }
70
71
    /**
72
     * Makes a request to the API to refresh the current access token
73
     * @see Client::getAccessToken()
74
     *
75
     * @return $this
76
     * @throws OAuthException
77
     */
78
    public function refreshAccessToken(): Connector
79
    {
80
        $this->_access_token = null;
81
        $this->getAccessToken();
82
        return $this;
83
    }
84
85
    /**
86
     * @param string $url
87
     * @param string $method
88
     * @param array $data
89
     * @param array $headers
90
     * @return CurlResponse
91
     * @throws OAuthException
92
     */
93
    public function curl_api(string $url, string $method = 'GET', array $data = [], array $headers = []): CurlResponse
94
    {
95
        $curl = new Curl($url, $data, $headers);
96
97
        $curl->setMethod($method);
98
        $curl->addHeader('access-token', $this->getAccessToken());
99
        $curl->addHeader('site-key', $this->_site_key);
100
        $curl->addHeader('Host', $this->_host);
101
        $curl->addHeader('Accept', 'application/json');
102
103
        return $curl->curl_json();
104
    }
105
106
    /**
107
     * @param string $path
108
     * @return string
109
     */
110
    protected function auth_url(string $path): string
111
    {
112
        return $this->_auth_domain . '/' . ltrim($path, '/');
113
    }
114
}
115