Completed
Pull Request — master (#1)
by Carsten
04:42
created

Client   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 76.47%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 9
c 1
b 1
f 0
lcom 1
cbo 1
dl 0
loc 92
ccs 26
cts 34
cp 0.7647
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 22 4
A shutdown() 0 6 2
B authenticate() 0 27 3
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 4
    public function __construct($secret_token = '', $grant_token = '')
33
    {
34
        // Check if lib cURL is enabled
35 4
        if (!function_exists('curl_init')) {
36
            throw new Exception('Lib cURL must be enabled on the server');
37
        }
38
39 4
        if (empty($secret_token)) {
40
            throw new Exception('secret token is missing');
41
        }
42
43 4
        if (empty($grant_token)) {
44
            throw new Exception('grant token is missing');
45
        }
46
47
        // Set auth string property
48 4
        $this->secret_token = $secret_token;
49 4
        $this->grant_token = $grant_token;
50
51
        // Instantiate cURL object with
52 4
        $this->authenticate();
53 4
    }
54
55
    /**
56
     * Shutdown function.
57
     *
58
     * Closes the current cURL connection
59
     */
60
    public function shutdown()
61
    {
62
        if (!empty($this->ch)) {
63
            curl_close($this->ch);
64
        }
65
    }
66
67
    /**
68
     * authenticate function.
69
     *
70
     * Create a cURL instance with authentication headers
71
     */
72 4
    protected function authenticate()
73
    {
74 4
        $this->ch = curl_init();
75
76
        $headers = [
77 4
            'Accept: application/json',
78 4
            'Content-Type: application/json; charset=utf-8',
79 4
        ];
80
81 4
        if (!empty($this->secret_token)) {
82 4
            $headers[] = 'X-AppSecretToken:'.$this->secret_token;
83 4
        }
84
85 4
        if (!empty($this->grant_token)) {
86 4
            $headers[] = 'X-AgreementGrantToken:'.$this->grant_token;
87 4
        }
88
89
        //default headers
90
        $options = [
91 4
            CURLOPT_RETURNTRANSFER => true,
92 4
            CURLOPT_SSL_VERIFYPEER => true,
93 4
            CURLOPT_HTTPAUTH       => CURLAUTH_BASIC,
94 4
            CURLOPT_HTTPHEADER     => $headers,
95 4
        ];
96
97 4
        curl_setopt_array($this->ch, $options);
98 4
    }
99
}
100