BaseService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 5
c 5
b 1
f 0
lcom 1
cbo 1
dl 0
loc 59
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B conectarServer() 0 38 4
1
<?php
2
3
namespace ByJG\WebService;
4
5
/**
6
 * Classe Base com todas as funcionalidades para acessar o serviço ByJG
7
 */
8
abstract class BaseService
9
{
10
    protected $URL         = "http://www.byjg.com.br/site/webservice.php/ws/";
11
    protected $_username   = "";
12
    protected $_password   = "";
13
    protected $_service    = "";
14
    protected $_curlParams = [];
15
16
    /**
17
     *
18
     * @param string $username
19
     * @param string $password
20
     */
21
    public function __construct($username, $password, $curlParams = [CURLOPT_TIMEOUT => 5])
22
    {
23
        $this->_username   = $username;
24
        $this->_password   = $password;
25
        $this->_curlParams = $curlParams;
26
    }
27
28
    protected function conectarServer($httpmethod, $params)
29
    {
30
        $params["httpmethod"] = $httpmethod;
31
        $params["usuario"]    = $this->_username;
32
        $params["senha"]      = $this->_password;
33
34
        $url = $this->URL . $this->_service;
35
36
        $webRequest = new \ByJG\Util\WebRequest($url);
37
38
        foreach ($this->_curlParams as $param => $value) {
39
            $webRequest->setCurlOption($param, $value);
40
        }
41
42
        $response = $webRequest->post($params);
43
44
        $firstData = explode('|', $response);
45
        $result    = array(
46
            'status' => $firstData[0],
47
            'raw'    => $response
48
        );
49
50
        if (isset($firstData[1])) {
51
            $parsedData = explode(', ', $firstData[1]);
52
53
            if (!isset($parsedData[1])) {
54
                $parsedData[0] = null;
55
                $parsedData[1] = $firstData[1];
56
            }
57
58
            $result['data'] = array(
59
                "code" => $parsedData[0],
60
                "info" => $parsedData[1]
61
            );
62
        }
63
64
        return $result;
65
    }
66
}
67