Passed
Push — master ( 1dadf3...61c4e2 )
by Fran
03:43
created

Service::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PSFS\base;
4
5
/**
6
 * Class Service
7
 * @package PSFS\base
8
 */
9
class Service extends Singleton
10
{
11
    /**
12
     * @var String Url de destino de la llamada
13
     */
14
    private $url;
15
    /**
16
     * @var array Parámetros de la llamada
17
     */
18
    private $params;
19
    /**
20
     * @var array Cabeceras de la llamada
21
     */
22
    private $headers;
23
    /**
24
     * @var string type
25
     */
26
    private $type;
27
    /**
28
     * @var resource $con
29
     */
30
    private $con;
31
    /**
32
     * @var string $result
33
     */
34
    private $result;
35
36
    /**
37
     * @Inyectable
38
     * @var \PSFS\base\Logger Log de las llamadas
39
     */
40
    protected $log;
41
    /**
42
     * @Inyectable
43
     * @var \PSFS\base\Cache $cache
44
     */
45
    protected $cache;
46
47
    /**
48
     * @return String
49
     */
50
    public function getUrl()
51
    {
52
        return $this->url;
53
    }
54
55
    /**
56
     * @param String $url
57
     */
58
    public function setUrl($url)
59
    {
60
        $this->url = $url;
61
        $this->initialize();
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getResult()
68
    {
69
        return $this->result;
70
    }
71
72
    /**
73
     * @param string $result
74
     */
75
    public function setResult($result)
76
    {
77
        $this->result = $result;
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function getParams()
84
    {
85
        return $this->params;
86
    }
87
88
    /**
89
     * Add request param
90
     *
91
     * @param $key
92
     * @param null $value
93
     *
94
     * @return \PSFS\base\Service
95
     */
96
    public function addParam($key, $value = NULL)
97
    {
98
        $this->params[$key] = $value;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @param array $params
105
     */
106
    public function setParams($params)
107
    {
108
        $this->params = $params;
109
    }
110
111
    /**
112
     * @return array
113
     */
114
    public function getHeaders()
115
    {
116
        return $this->headers;
117
    }
118
119
    /**
120
     * @param array $headers
121
     */
122
    public function setHeaders($headers)
123
    {
124
        $this->headers = $headers;
125
    }
126
127
    /**
128
     * @param $header
129
     * @param null $content
130
     *
131
     * @return $this
132
     */
133
    public function addHeader($header, $content = NULL)
134
    {
135
        $this->headers[$header] = $content;
136
137
        return $this;
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function getType()
144
    {
145
        return $this->type;
146
    }
147
148
    /**
149
     * @param string $type
150
     */
151
    public function setType($type)
152
    {
153
        $this->type = $type;
154
    }
155
156
    /**
157
     * @return Logger
158
     */
159
    public function getLog()
160
    {
161
        return $this->log;
162
    }
163
164
    /**
165
     * @param Logger $log
166
     */
167 1
    public function setLog($log)
168
    {
169 1
        $this->log = $log;
170 1
    }
171
172
    /**
173
     * Método que limpia el contexto de la llamada
174
     */
175 1
    private function clearContext()
176
    {
177 1
        $this->url = NULL;
178 1
        $this->params = array();
179 1
        $this->headers = array();
180 1
        Logger::log("Context service for " . get_called_class() . " cleared!");
181 1
    }
182
183
    /**
184
     *
185
     */
186 1
    public function init()
187
    {
188 1
        parent::init();
189 1
        $this->clearContext();
190 1
    }
191
192
    /**
193
     * Initialize CURL
194
     */
195
    private function initialize()
196
    {
197
        $this->con = curl_init($this->url);
198
    }
199
200
    /**
201
     * Generate auth header
202
     * @param string $secret
203
     */
204
    protected function addRequestToken($secret)
205
    {
206
        $this->addHeader('X-PSFS-SEC-TOKEN', Security::generateToken($secret));
207
    }
208
209
    protected function setOpts()
210
    {
211
        switch (strtoupper($this->type)) {
212
            case 'GET':
213
            default:
214
                curl_setopt($this->con, CURLOPT_CUSTOMREQUEST, "GET");
215
                break;
216 View Code Duplication
            case 'POST':
217
                curl_setopt($this->con, CURLOPT_CUSTOMREQUEST, "POST");
218
                curl_setopt($this->con, CURLOPT_POSTFIELDS, json_encode($this->params));
219
                break;
220
            case 'DELETE':
221
                curl_setopt($this->con, CURLOPT_CUSTOMREQUEST, "DELETE");
222
                break;
223 View Code Duplication
            case 'PUT':
224
                curl_setopt($this->con, CURLOPT_CUSTOMREQUEST, "PUT");
225
                curl_setopt($this->con, CURLOPT_POSTFIELDS, json_encode($this->params));
226
                break;
227 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...
228
                curl_setopt($this->con, CURLOPT_CUSTOMREQUEST, "PATCH");
229
                curl_setopt($this->con, CURLOPT_POSTFIELDS, json_encode($this->params));
230
                break;
231
        }
232
233
        curl_setopt($this->con, CURLOPT_RETURNTRANSFER, true);
234
        curl_setopt($this->con, CURLOPT_FOLLOWLOCATION, true);
235
    }
236
237
    public function callSrv()
238
    {
239
        $this->setOpts();
240
        $result = curl_exec($this->con);
241
        $this->result = json_decode($result);
242
    }
243
}
244