Passed
Push — master ( b111b6...d62ebd )
by
unknown
01:46
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
        // Save authentication string
50 10
        $this->auth_string = $auth_string;
51
52
        // Create cURL instance.
53 10
        $this->ch = curl_init();
54 10
        curl_setopt_array($this->ch, array(
55 10
            CURLOPT_RETURNTRANSFER => true,
56 10
            CURLOPT_SSL_VERIFYPEER => true,
57 10
            CURLOPT_HTTPAUTH => CURLAUTH_BASIC
58 10
        ));
59
60
        // Default headers
61 10
        $this->headers = array(
62 10
            'Accept-Version: v10',
63 10
            'Accept: application/json',
64
        );
65 10
        if (!empty($this->auth_string)) {
66 1
            $this->headers[] = 'Authorization: Basic ' . base64_encode($this->auth_string);
67 1
        }
68
69
        // Add custom headers and set headers in cURL object.
70 10
        $this->setHeaders($additional_headers);
71 10
    }
72
73
    /**
74
     * Shutdown function.
75
     *
76
     * Closes the current cURL connection
77
     *
78
     * @access public
79
     */
80
    public function shutdown()
81
    {
82
        if (!empty($this->ch)) {
83
            curl_close($this->ch);
84
        }
85
    }
86
87
    /**
88
     * Set additional headers for cURL request.
89
     *
90
     * @param string[] $additional_headers
91
     * @access public
92
     * @return bool
93
     */
94 10
    public function setHeaders($additional_headers)
95
    {
96 10
        if (!empty($additional_headers)) {
97 2
            $this->headers = array_merge($this->headers, $additional_headers);
98 2
        }
99 10
        return curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers);
100
    }
101
}
102