Passed
Pull Request — master (#57)
by
unknown
08:52
created

Client::setHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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
     * Contains the authentication string
23
     *
24
     * @access protected
25
     */
26
    protected $auth_string;
27
28
    /**
29
     * Contains the headers
30
     *
31
     * @access protected
32
     */
33
    protected $headers = array();
34
35
    /**
36
     * __construct function.
37
     *
38
     * Instantiate object
39
     *
40
     * @access public
41
     */
42 10
    public function __construct($auth_string = '', $additional_headers = array())
43
    {
44
        // Check if lib cURL is enabled
45 10
        if (!function_exists('curl_init')) {
46
            throw new Exception('Lib cURL must be enabled on the server');
47
        }
48
49
        // Set auth string property
50 10
        $this->auth_string = $auth_string;
51
52
        // Set headers
53 10
        $this->headers = array(
54 10
            'Accept-Version: v10',
55 10
            'Accept: application/json',
56
        );
57
58 10
        if (!empty($this->auth_string)) {
59 1
            $this->headers[] = 'Authorization: Basic ' . base64_encode($this->auth_string);
60 1
        }
61
62
        // Instantiate cURL object
63 10
        $this->authenticate();
64
65 10
        $this->setHeaders($additional_headers);
66 10
    }
67
68
    /**
69
     * Shutdown function.
70
     *
71
     * Closes the current cURL connection
72
     *
73
     * @access public
74
     */
75
    public function shutdown()
76
    {
77
        if (!empty($this->ch)) {
78
            curl_close($this->ch);
79
        }
80
    }
81
82
    /**
83
     * Set additinal headers to cURL
84
     *
85
     * @access public
86
     * @return boolean
87
     */
88 10
    public function setHeaders($additional_headers = array())
89
    {
90 10
        if (!empty($additional_headers)) {
91 2
            $this->headers = array_merge($this->headers, $additional_headers);
92 2
        }
93 10
        return curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers);
94
    }
95
96
    /**
97
     * authenticate function.
98
     *
99
     * Create a cURL instance with authentication headers
100
     *
101
     * @access public
102
     */
103 10
    protected function authenticate()
104
    {
105 10
        $this->ch = curl_init();
106
107
        $options = array(
108 10
            CURLOPT_RETURNTRANSFER => true,
109 10
            CURLOPT_SSL_VERIFYPEER => true,
110 10
            CURLOPT_HTTPAUTH => CURLAUTH_BASIC
111 10
        );
112
113 10
        curl_setopt_array($this->ch, $options);
114 10
    }
115
}
116