Passed
Push — master ( 412c28...27021d )
by Fran
04:12
created

Service::getOptions()   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 0
dl 0
loc 4
ccs 0
cts 2
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 1
    private function closeConnection() {
57 1
        if(null !== $this->con) {
58
            curl_close($this->con);
59
        }
60 1
    }
61
62
    public function __destruct()
63
    {
64
        $this->closeConnection();
65
    }
66
67
    /**
68
     * @return String
69
     */
70
    public function getUrl()
71
    {
72
        return $this->url;
73
    }
74
75
    /**
76
     * @param String $url
77
     */
78
    public function setUrl($url)
79
    {
80
        $this->url = $url;
81
        $this->initialize();
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getResult()
88
    {
89
        return $this->result;
90
    }
91
92
    /**
93
     * @param string $result
94
     */
95
    public function setResult($result)
96
    {
97
        $this->result = $result;
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    public function getParams()
104
    {
105
        return $this->params;
106
    }
107
108
    /**
109
     * Add request param
110
     *
111
     * @param $key
112
     * @param null $value
113
     *
114
     * @return \PSFS\base\Service
115
     */
116
    public function addParam($key, $value = NULL)
117
    {
118
        $this->params[$key] = $value;
119
120
        return $this;
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    public function getOptions()
127
    {
128
        return $this->options;
129
    }
130
131
    /**
132
     * Add request param
133
     *
134
     * @param $key
135
     * @param null $value
136
     *
137
     * @return \PSFS\base\Service
138
     */
139
    public function addOption($key, $value = NULL)
140
    {
141
        $this->options[$key] = $value;
142
143
        return $this;
144
    }
145
146
    /**
147
     * @param array $params
148
     */
149
    public function setParams($params)
150
    {
151
        $this->params = $params;
152
    }
153
154
    /**
155
     * @return array
156
     */
157
    public function getHeaders()
158
    {
159
        return $this->headers;
160
    }
161
162
    /**
163
     * @param array $headers
164
     */
165
    public function setHeaders($headers)
166
    {
167
        $this->headers = $headers;
168
    }
169
170
    /**
171
     * @param $header
172
     * @param null $content
173
     *
174
     * @return $this
175
     */
176
    public function addHeader($header, $content = NULL)
177
    {
178
        $this->headers[$header] = $content;
179
180
        return $this;
181
    }
182
183
    /**
184
     * @return string
185
     */
186
    public function getType()
187
    {
188
        return $this->type;
189
    }
190
191
    /**
192
     * @param string $type
193
     */
194
    public function setType($type)
195
    {
196
        $this->type = $type;
197
    }
198
199
    /**
200
     * @return Logger
201
     */
202
    public function getLog()
203
    {
204
        return $this->log;
205
    }
206
207
    /**
208
     * @param Logger $log
209
     */
210 1
    public function setLog($log)
211
    {
212 1
        $this->log = $log;
213 1
    }
214
215
    /**
216
     * Método que limpia el contexto de la llamada
217
     */
218 1
    private function clearContext()
219
    {
220 1
        $this->url = NULL;
221 1
        $this->params = array();
222 1
        $this->headers = array();
223 1
        Logger::log("Context service for " . get_called_class() . " cleared!");
224 1
        $this->closeConnection();
225 1
    }
226
227
    /**
228
     *
229
     */
230 1
    public function init()
231
    {
232 1
        parent::init();
233 1
        $this->clearContext();
234 1
    }
235
236
    /**
237
     * Initialize CURL
238
     */
239
    private function initialize()
240
    {
241
        $this->closeConnection();
242
        $this->con = curl_init($this->url);
243
    }
244
245
    /**
246
     * Generate auth header
247
     * @param string $secret
248
     * @param string $module
249
     */
250
    protected function addRequestToken($secret, $module = 'PSFS')
251
    {
252
        $this->addHeader('X-PSFS-SEC-TOKEN', SecurityHelper::generateToken($secret, $module));
253
    }
254
255
    /**
256
     * @param $user
257
     * @param $pass
258
     */
259
    protected function addAuthHeader($user, $pass) {
260
        $this->addOption(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
261
        $this->addOption(CURLOPT_USERPWD, "$user:$pass");
262
    }
263
264
    protected function applyOptions() {
265
        curl_setopt_array($this->con, $this->options);
266
    }
267
268
    protected function applyHeaders() {
269
        $headers = [];
270
        foreach($this->headers as $key => $value) {
271
            $headers[] = $key . ': ' . $value;
272
        }
273
        curl_setopt($this->con, CURLOPT_HTTPHEADER, $headers);
274
    }
275
276
    protected function setDefaults()
277
    {
278
        switch (strtoupper($this->type)) {
279
            case 'GET':
280
            default:
281
                $this->addOption(CURLOPT_CUSTOMREQUEST, "GET");
282
                break;
283 View Code Duplication
            case 'POST':
284
                $this->addOption(CURLOPT_CUSTOMREQUEST, "POST");
285
                $this->addOption(CURLOPT_POSTFIELDS, json_encode($this->params));
286
                break;
287
            case 'DELETE':
288
                $this->addOption(CURLOPT_CUSTOMREQUEST, "DELETE");
289
                break;
290 View Code Duplication
            case 'PUT':
291
                $this->addOption(CURLOPT_CUSTOMREQUEST, "PUT");
292
                $this->addOption(CURLOPT_POSTFIELDS, json_encode($this->params));
293
                break;
294 View Code Duplication
            case 'PATCH':
295
                $this->addOption(CURLOPT_CUSTOMREQUEST, "PATCH");
296
                $this->addOption(CURLOPT_POSTFIELDS, json_encode($this->params));
297
                break;
298
        }
299
300
        $this->addOption(CURLOPT_RETURNTRANSFER, true);
301
        $this->addOption(CURLOPT_FOLLOWLOCATION, true);
302
    }
303
304
    public function callSrv()
305
    {
306
        $this->setDefaults();
307
        $this->applyOptions();
308
        $this->applyHeaders();
309
        $result = curl_exec($this->con);
310
        $this->result = json_decode($result, true);
311
        Logger::log($this->url . ' response: ', LOG_DEBUG, $this->result);
312
        $this->info = curl_getinfo($this->con);
313
    }
314
315
    /**
316
     * @return mixed
317
     */
318
    public function getCallInfo() {
319
        return $this->info;
320
    }
321
}
322