Client   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 52
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A createLabel() 0 4 1
B sendRequest() 0 30 2
1
<?php
2
3
namespace OceanApplications\Postmen;
4
5
use OceanApplications\Postmen\Requests\Label;
6
7
class Client
8
{
9
    private $apiKey = '';
10
    private $baseUrl = '';
11
12
    public function __construct($apiKey, $sandbox = false)
13
    {
14
        $this->apiKey = $apiKey;
15
16
        if ($sandbox == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
17
            $this->baseUrl = 'https://sandbox-api.postmen.com/v3/';
18
        } else {
19
            $this->baseUrl = 'https://production-api.postmen.com/v3/';
20
        }
21
    }
22
23
    public function createLabel(Label $label)
24
    {
25
        return $this->sendRequest($label, 'labels');
26
    }
27
28
    private function sendRequest($data, $endpoint)
29
    {
30
        $method = 'POST';
31
        $headers = [
32
            'content-type: application/json',
33
            'postmen-api-key: '.$this->apiKey,
34
        ];
35
        $body = json_encode($data);
36
37
        $curl = curl_init();
38
        curl_setopt_array($curl, [
39
            CURLOPT_RETURNTRANSFER => true,
40
            CURLOPT_URL            => $this->baseUrl.$endpoint,
41
            CURLOPT_CUSTOMREQUEST  => $method,
42
            CURLOPT_HTTPHEADER     => $headers,
43
            CURLOPT_POSTFIELDS     => $body,
44
            CURLOPT_SSL_VERIFYPEER => false,
45
        ]);
46
47
        $response = curl_exec($curl);
48
        $err = curl_error($curl);
49
50
        curl_close($curl);
51
52
        if ($err) {
53
            echo 'cURL Error #:'.$err;
54
        } else {
55
            return $response;
56
        }
57
    }
58
}
59