BaseAdapter::getClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Habrahabr\Api\HttpAdapter;
4
5
use Habrahabr\Api\Exception\IncorrectUsageException;
6
7
/**
8
 * Class BaseAdapter
9
 *
10
 * Базовый класс для всех Habrahabr Api HTTP адаптеров
11
 *
12
 * @package Habrahabr\Api\HttpAdapter
13
 * @version 0.1.5
14
 * @author thematicmedia <[email protected]>
15
 * @link https://tmtm.ru/
16
 * @link https://habrahabr.ru/
17
 * @link https://github.com/thematicmedia/habrahabr_api
18
 *
19
 * For the full copyright and license information, please view the LICENSE
20
 * file that was distributed with this source code.
21
 */
22
abstract class BaseAdapter
23
{
24
    /**
25
     * @var null|string Ключ для доступа к не персонализированным данным API
26
     */
27
    protected $apikey = null;
28
29
    /**
30
     * @var null|string OAuth Token для доступа к API
31
     */
32
    protected $token = null;
33
34
    /**
35
     * @var null|string OAuth Client для доступа к API
36
     */
37
    protected $client = null;
38
39
    /**
40
     * @var null|string OAuth Endpoint для доступа к API
41
     */
42
    protected $endpoint;
43
44
    /**
45
     * @var int Количество секунд ожидания при попытке соединения
46
     */
47
    protected $connectionTimeout = 5;
48
49
    /**
50
     * Установить ключ для доступа к не персонализированным данным API
51
     *
52
     * @param null|string $apikey Ключ для доступа к не персонализированным данным API
53
     * @return $this
54
     * @throws IncorrectUsageException
55
     */
56 2
    public function setApikey($apikey)
57
    {
58 2
        if (!preg_match('#([a-z0-9]+)#ui', $apikey)) {
59 1
            throw new IncorrectUsageException('Incorrect API Key');
60
        }
61
62 1
        $this->apikey = $apikey;
63
64 1
        return $this;
65
    }
66
67
    /**
68
     * Установить OAuth Token для доступа к API
69
     *
70
     * @param null|string $token OAuth Token для доступа к API
71
     * @return $this
72
     * @throws IncorrectUsageException
73
     */
74 2
    public function setToken($token)
75
    {
76 2
        if (!preg_match('#([a-z0-9]+)#ui', $token)) {
77 1
            throw new IncorrectUsageException('Incorrect API Token');
78
        }
79
80 1
        $this->token = $token;
81
82 1
        return $this;
83
    }
84
85
    /**
86
     * Установить OAuth Client для доступа к API
87
     *
88
     * @param null|string $client OAuth Client для доступа к API
89
     * @return $this
90
     * @throws IncorrectUsageException
91
     */
92 2
    public function setClient($client)
93
    {
94 2
        if (!preg_match('#([a-z0-9]+)\.([a-z0-9]+)#ui', $client)) {
95 1
            throw new IncorrectUsageException('Incorrect API Client');
96
        }
97
98 1
        $this->client = $client;
99
100 1
        return $this;
101
    }
102
103
    /**
104
     * Получить OAuth Client для доступа к API
105
     *
106
     * @return null|string
107
     */
108 2
    public function getClient()
109
    {
110 2
        return $this->client;
111
    }
112
113
    /**
114
     * Установить OAuth Endpoint для доступа к API
115
     *
116
     * @param null|string $url OAuth Endpoint для доступа к API
117
     * @return $this
118
     * @throws IncorrectUsageException
119
     */
120 9
    public function setEndpoint($url)
121
    {
122 9
        if (!preg_match('#^(https://)#ui', $url)) {
123 1
            throw new IncorrectUsageException('Scheme of endpoint must be HTTPS');
124
        }
125
126 8
        $this->endpoint = rtrim($url, '/');
127
128 8
        return $this;
129
    }
130
131
    /**
132
     * Получить OAuth Endpoint для доступа к API
133
     *
134
     * @return null|string
135
     */
136 7
    public function getEndpoint()
137
    {
138 7
        return $this->endpoint;
139
    }
140
141
    /**
142
     * Установить количество секунд ожидания при попытке соединения
143
     *
144
     * @param int $connectionTimeout Количество секунд ожидания при попытке соединения
145
     * @return $this
146
     */
147 1
    public function setConnectionTimeout($connectionTimeout = 0)
148
    {
149 1
        $this->connectionTimeout = $connectionTimeout;
150
151 1
        return $this;
152
    }
153
154
    /**
155
     * Получить количество секунд ожидания при попытке соединения
156
     *
157
     * @return int
158
     */
159 1
    public function getConnectionTimeout()
160
    {
161 1
        return $this->connectionTimeout;
162
    }
163
164
    /**
165
     * Создание URL на базе OAuth Endpoint и URL ресурса
166
     *
167
     * @param string $url URL ресурса
168
     * @return string
169
     */
170 5
    public function createUrl($url)
171
    {
172 5
        return $this->getEndpoint() . $url;
173
    }
174
}
175