Passed
Pull Request — master (#52)
by Lars
01:43
created

Client::shutdown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
namespace QuickPay\API;
3
4
/**
5
 * @class       QuickPay_Client
6
 * @since       0.1.0
7
 * @package     QuickPay
8
 * @category    Class
9
 * @author      Patrick Tolvstein, Perfect Solution ApS
10
 * @docs        http://tech.quickpay.net/api/
11
 */
12
class Client
13
{
14
    /**
15
     * Contains cURL instance
16
     *
17
     * @access public
18
     */
19
    public $ch;
20
21
    /**
22
     * Base url for the selected API.
23
     *
24
     * @var string
25
     */
26
    public $base_url;
27
28
    /**
29
     * Contains the authentication string
30
     *
31
     * @access protected
32
     */
33
    protected $auth_string;
34
35
    /**
36
     * __construct function.
37
     *
38
     * Instantiate object
39
     *
40
     * @access public
41
     * @param string $auth_string   Format 'username:password' or ':apiKey'
42
     * @param string $base_url      The API to call. Use on of the constants.
43
     * @throws Exception
44
     */
45 6
    public function __construct($auth_string = '', $base_url = Constants::API_URL)
46
    {
47
        // Check if lib cURL is enabled
48 6
        if (!function_exists('curl_init')) {
49
            throw new Exception('Lib cURL must be enabled on the server');
50
        }
51
52
        // Set auth string property
53 6
        $this->auth_string = $auth_string;
54
55
        // Set base url of selected API
56 6
        $this->base_url =  $base_url;
57
58
        // Instantiate cURL object
59 6
        $this->authenticate();
60 6
    }
61
62
    /**
63
     * Shutdown function.
64
     *
65
     * Closes the current cURL connection
66
     *
67
     * @access public
68
     */
69
    public function shutdown()
70
    {
71
        if (!empty($this->ch)) {
72
            curl_close($this->ch);
73
        }
74
    }
75
76
    /**
77
     * authenticate function.
78
     *
79
     * Create a cURL instance with authentication headers
80
     *
81
     * @access public
82
     */
83 6
    protected function authenticate()
84
    {
85 6
        $this->ch = curl_init();
86
87 6
        $headers = array();
88 6
        switch ($this->base_url) {
89 6
            case Constants::API_URL_INVOICING:
90
                $headers[] = 'Accept: application/vnd.api+json';
91
                break;
92
93 6
            case Constants::API_URL:
94 6
                $headers[] = 'Accept-Version: v' . Constants::API_VERSION;
95 6
                $headers[] = 'Accept: application/json';
96 6
                break;
97
98
            default:
99
                break;
100 6
        }
101
102 6
        if (!empty($this->auth_string)) {
103 1
            $headers[] = 'Authorization: Basic ' . base64_encode($this->auth_string);
104 1
        }
105
106
        $options = array(
107 6
            CURLOPT_RETURNTRANSFER => true,
108 6
            CURLOPT_SSL_VERIFYPEER => true,
109 6
            CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
110 6
            CURLOPT_HTTPHEADER => $headers
111 6
        );
112
113 6
        curl_setopt_array($this->ch, $options);
114 6
    }
115
}
116