Passed
Push — master ( addfa6...29b3e0 )
by Carsten
01:51
created

Client::shutdown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Lenius\Economic\API;
4
5
/**
6
 * @class       Economic_Client
7
 */
8
class Client
9
{
10
    /**
11
     * Contains cURL instance.
12
     */
13
    public $ch;
14
15
    /**
16
     * Contains the authentication string.
17
     */
18
    protected $secret_token;
19
20
    protected $grant_token;
21
22
    /**
23
     * __construct function.
24
     *
25
     * Instantiate object
26
     *
27
     * @param string $secret_token
28
     * @param string $grant_token
29
     *
30
     * @throws Exception
31
     */
32 15
    public function __construct($secret_token = '', $grant_token = '')
33
    {
34
        // Check if lib cURL is enabled
35 15
        if (!function_exists('curl_init')) {
36
            throw new Exception('Lib cURL must be enabled on the server');
37
        }
38
39 15
        if (empty($secret_token)) {
40 1
            throw new Exception('secret token is missing');
41
        }
42
43 14
        if (empty($grant_token)) {
44 1
            throw new Exception('grant token is missing');
45
        }
46
47
        // Set auth string property
48 13
        $this->secret_token = $secret_token;
49 13
        $this->grant_token = $grant_token;
50 13
    }
51
52
    /**
53
     * Shutdown function.
54
     *
55
     * Closes the current cURL connection
56
     */
57 8
    public function shutdown()
58
    {
59 8
        if (!empty($this->ch)) {
60 8
            curl_close($this->ch);
61
        }
62 8
    }
63
64
    /**
65
     * Create function.
66
     *
67
     * Create cURL connection with authentication
68
     */
69 8
    public function create()
70
    {
71 8
        if (!empty($this->ch)) {
72
            curl_close($this->ch);
73
        }
74
75
        // Instantiate cURL object
76 8
        $this->ch = curl_init();
77
78
        // Apply authentication headers
79 8
        $this->authenticate();
80 8
    }
81
82
    /**
83
     * authenticate function.
84
     *
85
     * Create authentication headers
86
     */
87 8
    protected function authenticate()
88
    {
89
        $headers = [
90 8
            'Accept: application/json',
91
            'Content-Type: application/json; charset=utf-8',
92
        ];
93
94 8
        if (!empty($this->secret_token)) {
95 8
            $headers[] = 'X-AppSecretToken:'.$this->secret_token;
96
        }
97
98 8
        if (!empty($this->grant_token)) {
99 8
            $headers[] = 'X-AgreementGrantToken:'.$this->grant_token;
100
        }
101
102
        //default headers
103
        $options = [
104 8
            CURLOPT_RETURNTRANSFER => true,
105 8
            CURLOPT_SSL_VERIFYPEER => true,
106 8
            CURLOPT_HTTPAUTH       => CURLAUTH_BASIC,
107 8
            CURLOPT_HTTPHEADER     => $headers,
108
        ];
109
110 8
        curl_setopt_array($this->ch, $options);
111 8
    }
112
}
113