Passed
Push — master ( 1c0893...f7ffcc )
by Fran
03:22
created

Service::initialize()   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 3
cp 0
crap 2
rs 10
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
     * Constructor por defecto
185
     */
186 1
    public function __construct()
187
    {
188 1
        parent::__construct();
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
        }
228
229
        curl_setopt($this->con, CURLOPT_RETURNTRANSFER, true);
230
        curl_setopt($this->con, CURLOPT_FOLLOWLOCATION, true);
231
    }
232
233
    public function callSrv()
234
    {
235
        $this->setOpts();
236
        $result = curl_exec($this->con);
237
        $this->result = json_decode($result);
238
    }
239
}
240