Completed
Push — master ( 842d90...49bad6 )
by dotzero
05:00
created

BaseAdapter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 1 Features 1
Metric Value
wmc 11
c 8
b 1
f 1
lcom 1
cbo 1
dl 0
loc 130
ccs 26
cts 26
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setToken() 0 10 2
A setClient() 0 10 2
A getClient() 0 4 1
A setEndpoint() 0 10 2
A getEndpoint() 0 4 1
A setConnectionTimeout() 0 6 1
A getConnectionTimeout() 0 4 1
A createUrl() 0 4 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.0
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 OAuth Token для доступа к API
26
     */
27
    protected $token = null;
28
29
    /**
30
     * @var null|string OAuth Client для доступа к API
31
     */
32
    protected $client = null;
33
34
    /**
35
     * @var null|string OAuth Endpoint для доступа к API
36
     */
37
    protected $endpoint;
38
39
    /**
40
     * @var int Количество секунд ожидания при попытке соединения
41
     */
42
    protected $connectionTimeout = 5;
43
44
    /**
45
     * Установить OAuth Token для доступа к API
46
     *
47
     * @param null|string $token OAuth Token для доступа к API
48
     * @return $this
49
     * @throws IncorrectUsageException
50
     */
51 2
    public function setToken($token)
52
    {
53 2
        if (!preg_match('#([a-z0-9]+)#ui', $token)) {
54 1
            throw new IncorrectUsageException('Incorrect API Token');
55
        }
56
57 1
        $this->token = $token;
58
59 1
        return $this;
60
    }
61
62
    /**
63
     * Установить OAuth Client для доступа к API
64
     *
65
     * @param null|string $client OAuth Client для доступа к API
66
     * @return $this
67
     * @throws IncorrectUsageException
68
     */
69 2
    public function setClient($client)
70
    {
71 2
        if (!preg_match('#([a-z0-9]+)\.([a-z0-9]+)#ui', $client)) {
72 1
            throw new IncorrectUsageException('Incorrect API Client');
73
        }
74
75 1
        $this->client = $client;
76
77 1
        return $this;
78
    }
79
80
    /**
81
     * Получить OAuth Client для доступа к API
82
     *
83
     * @return null|string
84
     */
85 2
    public function getClient()
86
    {
87 2
        return $this->client;
88
    }
89
90
    /**
91
     * Установить OAuth Endpoint для доступа к API
92
     *
93
     * @param null|string $url OAuth Endpoint для доступа к API
94
     * @return $this
95
     * @throws IncorrectUsageException
96
     */
97 9
    public function setEndpoint($url)
98
    {
99 9
        if (!preg_match('#^(https://)#ui', $url)) {
100 1
            throw new IncorrectUsageException('Scheme of endpoint must be HTTPS');
101
        }
102
103 8
        $this->endpoint = rtrim($url, '/');
104
105 8
        return $this;
106
    }
107
108
    /**
109
     * Получить OAuth Endpoint для доступа к API
110
     *
111
     * @return null|string
112
     */
113 7
    public function getEndpoint()
114
    {
115 7
        return $this->endpoint;
116
    }
117
118
    /**
119
     * Установить количество секунд ожидания при попытке соединения
120
     *
121
     * @param int $connectionTimeout Количество секунд ожидания при попытке соединения
122
     * @return $this
123
     */
124 1
    public function setConnectionTimeout($connectionTimeout = 0)
125
    {
126 1
        $this->connectionTimeout = $connectionTimeout;
127
128 1
        return $this;
129
    }
130
131
    /**
132
     * Получить количество секунд ожидания при попытке соединения
133
     *
134
     * @return int
135
     */
136 1
    public function getConnectionTimeout()
137
    {
138 1
        return $this->connectionTimeout;
139
    }
140
141
    /**
142
     * Создание URL на базе OAuth Endpoint и URL ресурса
143
     *
144
     * @param string $url URL ресурса
145
     * @return string
146
     */
147 5
    public function createUrl($url)
148
    {
149 5
        return $this->getEndpoint() . $url;
150
    }
151
}
152