Passed
Push — master ( 2dd92a...03eae4 )
by Fran
04:40
created

Service::addRequestToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace PSFS\base;
3
4
use PSFS\base\types\helpers\SecurityHelper;
5
6
/**
7
 * Class Service
8
 * @package PSFS\base
9
 */
10
class Service extends Singleton
11
{
12
    /**
13
     * @var String Url de destino de la llamada
14
     */
15
    private $url;
16
    /**
17
     * @var array Parámetros de la llamada
18
     */
19
    private $params;
20
    /**
21
     * @var array Opciones llamada
22
     */
23
    private $options;
24
    /**
25
     * @var array Cabeceras de la llamada
26
     */
27
    private $headers;
28
    /**
29
     * @var string type
30
     */
31
    private $type;
32
    /**
33
     * @var resource $con
34
     */
35
    private $con;
36
    /**
37
     * @var string $result
38
     */
39
    private $result;
40
    /**
41
     * @var mixed
42
     */
43
    private $info;
44
45
    /**
46
     * @Inyectable
47
     * @var \PSFS\base\Logger Log de las llamadas
48
     */
49
    protected $log;
50
    /**
51
     * @Inyectable
52
     * @var \PSFS\base\Cache $cache
53
     */
54
    protected $cache;
55
    /**
56
     * @var bool
57
     */
58
    protected $isJson = true;
59
60 1
    private function closeConnection() {
61 1
        if(null !== $this->con) {
62
            curl_close($this->con);
63
        }
64 1
    }
65
66
    public function __destruct()
67
    {
68
        $this->closeConnection();
69
    }
70
71
    /**
72
     * @return String
73
     */
74
    public function getUrl()
75
    {
76
        return $this->url;
77
    }
78
79
    /**
80
     * @param String $url
81
     */
82
    public function setUrl($url)
83
    {
84
        $this->url = $url;
85
        $this->initialize();
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getResult()
92
    {
93
        return $this->result;
94
    }
95
96
    /**
97
     * @param string $result
98
     */
99
    public function setResult($result)
100
    {
101
        $this->result = $result;
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    public function getParams()
108
    {
109
        return $this->params;
110
    }
111
112
    /**
113
     * Add request param
114
     *
115
     * @param $key
116
     * @param null $value
117
     *
118
     * @return \PSFS\base\Service
119
     */
120
    public function addParam($key, $value = NULL)
121
    {
122
        $this->params[$key] = $value;
123
124
        return $this;
125
    }
126
127
    /**
128
     * @return array
129
     */
130
    public function getOptions()
131
    {
132
        return $this->options;
133
    }
134
135
    /**
136
     * Add request param
137
     *
138
     * @param $key
139
     * @param null $value
140
     *
141
     * @return \PSFS\base\Service
142
     */
143
    public function addOption($key, $value = NULL)
144
    {
145
        $this->options[$key] = $value;
146
147
        return $this;
148
    }
149
150
    /**
151
     * @param array $params
152
     */
153
    public function setParams($params)
154
    {
155
        $this->params = $params;
156
    }
157
158
    /**
159
     * @return array
160
     */
161
    public function getHeaders()
162
    {
163
        return $this->headers;
164
    }
165
166
    /**
167
     * @param array $headers
168
     */
169
    public function setHeaders($headers)
170
    {
171
        $this->headers = $headers;
172
    }
173
174
    /**
175
     * @param $header
176
     * @param null $content
177
     *
178
     * @return $this
179
     */
180
    public function addHeader($header, $content = NULL)
181
    {
182
        $this->headers[$header] = $content;
183
184
        return $this;
185
    }
186
187
    /**
188
     * @return string
189
     */
190
    public function getType()
191
    {
192
        return $this->type;
193
    }
194
195
    /**
196
     * @param string $type
197
     */
198
    public function setType($type)
199
    {
200
        $this->type = $type;
201
    }
202
203
    /**
204
     * @return Logger
205
     */
206
    public function getLog()
207
    {
208
        return $this->log;
209
    }
210
211
    /**
212
     * @param Logger $log
213
     */
214 1
    public function setLog($log)
215
    {
216 1
        $this->log = $log;
217 1
    }
218
219
    /**
220
     * @param bool $isJson
221
     */
222
    public function setIsJson($isJson = true) {
223
        $this->isJson = $isJson;
224
    }
225
226
    /**
227
     * @return bool
228
     */
229
    public function getIsJson() {
230
        return $this->isJson;
231
    }
232
233
    /**
234
     * Método que limpia el contexto de la llamada
235
     */
236 1
    private function clearContext()
237
    {
238 1
        $this->url = NULL;
239 1
        $this->params = array();
240 1
        $this->headers = array();
241 1
        Logger::log("Context service for " . get_called_class() . " cleared!");
242 1
        $this->closeConnection();
243 1
    }
244
245
    /**
246
     *
247
     */
248 1
    public function init()
249
    {
250 1
        parent::init();
251 1
        $this->clearContext();
252 1
    }
253
254
    /**
255
     * Initialize CURL
256
     */
257
    private function initialize()
258
    {
259
        $this->closeConnection();
260
        $this->con = curl_init($this->url);
261
    }
262
263
    /**
264
     * Generate auth header
265
     * @param string $secret
266
     * @param string $module
267
     */
268
    protected function addRequestToken($secret, $module = 'PSFS')
269
    {
270
        $this->addHeader('X-PSFS-SEC-TOKEN', SecurityHelper::generateToken($secret, $module));
271
    }
272
273
    /**
274
     * @param $user
275
     * @param $pass
276
     */
277
    protected function addAuthHeader($user, $pass) {
278
        $this->addOption(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
279
        $this->addOption(CURLOPT_USERPWD, "$user:$pass");
280
    }
281
282
    protected function applyOptions() {
283
        curl_setopt_array($this->con, $this->options);
284
    }
285
286
    protected function applyHeaders() {
287
        $headers = [];
288
        foreach($this->headers as $key => $value) {
289
            $headers[] = $key . ': ' . $value;
290
        }
291
        curl_setopt($this->con, CURLOPT_HTTPHEADER, $headers);
292
    }
293
294
    protected function setDefaults()
295
    {
296
        switch (strtoupper($this->type)) {
297
            case 'GET':
298
            default:
299
                $this->addOption(CURLOPT_CUSTOMREQUEST, "GET");
300
                break;
301 View Code Duplication
            case 'POST':
302
                $this->addOption(CURLOPT_CUSTOMREQUEST, "POST");
303
                if($this->getIsJson()) {
304
                    $this->addOption(CURLOPT_POSTFIELDS, json_encode($this->params));
305
                } else {
306
                    $this->addOption(CURLOPT_POSTFIELDS, http_build_query($this->params));
307
                }
308
                break;
309
            case 'DELETE':
310
                $this->addOption(CURLOPT_CUSTOMREQUEST, "DELETE");
311
                break;
312 View Code Duplication
            case 'PUT':
313
                $this->addOption(CURLOPT_CUSTOMREQUEST, "PUT");
314
315
                if($this->getIsJson()) {
316
                    $this->addOption(CURLOPT_POSTFIELDS, json_encode($this->params));
317
                } else {
318
                    $this->addOption(CURLOPT_POSTFIELDS, http_build_query($this->params));
319
                }
320
                break;
321 View Code Duplication
            case 'PATCH':
322
                $this->addOption(CURLOPT_CUSTOMREQUEST, "PATCH");
323
                if($this->getIsJson()) {
324
                    $this->addOption(CURLOPT_POSTFIELDS, json_encode($this->params));
325
                } else {
326
                    $this->addOption(CURLOPT_POSTFIELDS, http_build_query($this->params));
327
                }
328
                break;
329
        }
330
331
        $this->addOption(CURLOPT_RETURNTRANSFER, true);
332
        $this->addOption(CURLOPT_FOLLOWLOCATION, true);
333
    }
334
335
    public function callSrv()
336
    {
337
        $this->setDefaults();
338
        $this->applyOptions();
339
        $this->applyHeaders();
340
        $result = curl_exec($this->con);
341
        $this->result = json_decode($result, true);
342
        Logger::log($this->url . ' response: ', LOG_DEBUG, $this->result);
343
        $this->info = curl_getinfo($this->con);
344
    }
345
346
    /**
347
     * @return mixed
348
     */
349
    public function getCallInfo() {
350
        return $this->info;
351
    }
352
}
353