ClientHttpful::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Rutube PHP API Client package.
5
 *
6
 * (c) Rutube
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Rutube\Clients;
13
14
use Httpful\Request;
15
16
/**
17
 * HTTP-клиент на основе Httpful
18
 * @package Rutube\Clients
19
 */
20
class ClientHttpful implements ClientInterface
21
{
22
    /**
23
     * Таймаут HTTP-запроса
24
     */
25
    const TIMEOUT = 60;
26
27
    /**
28
     * @var string|null Пользовательский User-Agent
29
     */
30
    protected $userAgent = null;
31
32
    /**
33
     * @var string|null Передавать в заголовке X-Real-IP указанный IP-адрес
34
     */
35
    protected $x_real_ip = null;
36
37
    /**
38
     * @var Request
39
     */
40
    protected $request;
41
42
    /**
43
     * Запрос GET
44
     * @param string $uri
45
     * @return mixed
46
     */
47
    public function get($uri)
48
    {
49
        $this->request = Request::get($uri);
50
51
        return $this;
52
    }
53
54
    /**
55
     * Запрос POST
56
     * @param string $uri
57
     * @return mixed
58
     */
59
    public function post($uri)
60
    {
61
        $this->request = Request::post($uri);
62
63
        return $this;
64
    }
65
66
    /**
67
     * Заропс PUT
68
     * @param string $uri
69
     * @return mixed
70
     */
71
    public function put($uri)
72
    {
73
        $this->request = Request::put($uri);
74
75
        return $this;
76
    }
77
78
    /**
79
     * Запрос DELETE
80
     * @param string $uri
81
     * @return mixed
82
     */
83
    public function delete($uri)
84
    {
85
        $this->request = Request::delete($uri);
86
87
        return $this;
88
    }
89
90
    /**
91
     * Запрос PATCH
92
     * @param $uri
93
     * @return mixed
94
     */
95
    public function patch($uri)
96
    {
97
        $this->request = Request::patch($uri);
98
99
        return $this;
100
    }
101
102
    /**
103
     * Установка заголовков для корректной работы с Rutube
104
     * @param string $token
105
     * @return mixed
106
     */
107
    public function setHeaders($token = null)
108
    {
109
        $headers = array(
110
            'Accept' => 'application/json',
111
            'User-Agent' => 'Rutube_PHPClient',
112
        );
113
114
        if ($token !== null) {
115
            $headers['Authorization'] = 'Token ' . $token;
116
        }
117
118
        $this->request->addHeaders($headers);
119
120
        return $this;
121
    }
122
123
    /**
124
     * Тело запроса
125
     * @param array $body
126
     * @return mixed
127
     */
128
    public function setBody($body)
129
    {
130
        $this->request->body($body);
131
132
        return $this;
133
    }
134
135
    /**
136
     * Выполнение запроса
137
     * @return \Httpful\associative|string
138
     *
139
     * @throws \Httpful\Exception\ConnectionErrorException
140
     */
141
    public function send()
142
    {
143
        if ($this->userAgent !== null) {
144
            $this->request->addHeader('User-Agent', $this->userAgent);
145
        }
146
147
        if ($this->x_real_ip !== null) {
148
            $this->request->addHeader('X-Real-IP', $this->x_real_ip);
149
        }
150
151
        return $this->request->timeout(self::TIMEOUT)->send();
152
    }
153
154
    /**
155
     * Выставление заголовка принятия ответа в формате Json
156
     * @return $this
157
     */
158
    public function asJson()
159
    {
160
        $this->request->sendsJson();
161
162
        return $this;
163
    }
164
165
    /**
166
     * Отсылка файла через POST-запрос
167
     * @param array $files
168
     * @return mixed
169
     */
170
    public function attach(array $files)
171
    {
172
        $this->request->attach($files);
173
174
        return $this;
175
    }
176
177
    /**
178
     * Устанавливает User-Agent для текущего запроса
179
     * @param string $userAgent
180
     * @return $this
181
     */
182
    public function setUserAgent($userAgent)
183
    {
184
        $this->userAgent = $userAgent;
185
186
        return $this;
187
    }
188
189
    /**
190
     * Установка клиентского IP-адреса
191
     * @param string $ip
192
     * @return mixed
193
     */
194
    public function setXRealIP($ip)
195
    {
196
        $this->x_real_ip = $ip;
197
198
        return $this;
199
    }
200
}
201