Passed
Push — master ( bfb8e2...d7aeee )
by Fran
04:12
created

Service::getCallInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PSFS\base;
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 setDefaults()
269
    {
270
        switch (strtoupper($this->type)) {
271
            case 'GET':
272
            default:
273
                $this->addOption(CURLOPT_CUSTOMREQUEST, "GET");
274
                break;
275 View Code Duplication
            case 'POST':
276
                $this->addOption(CURLOPT_CUSTOMREQUEST, "POST");
277
                $this->addOption(CURLOPT_POSTFIELDS, json_encode($this->params));
278
                break;
279
            case 'DELETE':
280
                $this->addOption(CURLOPT_CUSTOMREQUEST, "DELETE");
281
                break;
282 View Code Duplication
            case 'PUT':
283
                $this->addOption(CURLOPT_CUSTOMREQUEST, "PUT");
284
                $this->addOption(CURLOPT_POSTFIELDS, json_encode($this->params));
285
                break;
286 View Code Duplication
            case 'PATCH':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
287
                $this->addOption(CURLOPT_CUSTOMREQUEST, "PATCH");
288
                $this->addOption(CURLOPT_POSTFIELDS, json_encode($this->params));
289
                break;
290
        }
291
292
        $this->addOption(CURLOPT_RETURNTRANSFER, true);
293
        $this->addOption(CURLOPT_FOLLOWLOCATION, true);
294
    }
295
296
    public function callSrv()
297
    {
298
        $this->setDefaults();
299
        $this->applyOptions();
300
        $result = curl_exec($this->con);
301
        $this->result = json_decode($result, true);
302
        $this->info = curl_getinfo($this->con);
303
    }
304
305
    /**
306
     * @return mixed
307
     */
308
    public function getCallInfo() {
309
        return $this->info;
310
    }
311
}
312