Completed
Pull Request — master (#52)
by Lars
05:00 queued 02:37
created

Client::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2.0116
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 7
    public function __construct($auth_string = '', $base_url = Constants::API_URL)
46
    {
47
        // Check if lib cURL is enabled
48 7
        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 7
        $this->auth_string = $auth_string;
54
55
        // Set base url of selected API
56 7
        $this->base_url =  $base_url;
57
58
        // Instantiate cURL object
59 7
        $this->authenticate();
60 7
    }
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 7
    protected function authenticate()
84
    {
85 7
        $this->ch = curl_init();
86
87 7
        $headers = array();
88 7
        switch ($this->base_url) {
89 7
            case Constants::API_URL_INVOICING:
90
                $headers[] = 'Accept: application/vnd.api+json';
91
                break;
92
93 7
            case Constants::API_URL:
94 7
                $headers[] = 'Accept-Version: v' . Constants::API_VERSION;
95 7
                $headers[] = 'Accept: application/json';
96 7
                break;
97
98
            default:
99
                break;
100 7
        }
101
102 7
        if (!empty($this->auth_string)) {
103 1
            $headers[] = 'Authorization: Basic ' . base64_encode($this->auth_string);
104 1
        }
105
106
        $options = array(
107 7
            CURLOPT_RETURNTRANSFER => true,
108 7
            CURLOPT_SSL_VERIFYPEER => true,
109 7
            CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
110 7
            CURLOPT_HTTPHEADER => $headers
111 7
        );
112
113 7
        curl_setopt_array($this->ch, $options);
114 7
    }
115
}
116