Passed
Push — master ( 58ddad...5e153e )
by Fran
03:31
created

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