Passed
Push — master ( de71cb...6050eb )
by Carsten
02:32
created

Client   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 108
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

4 Methods

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