Passed
Push — master ( c22b6a...5f79f5 )
by Julito
11:21
created

JWTClient::send()   B

Complexity

Conditions 11
Paths 21

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 35
c 0
b 0
f 0
nc 21
nop 4
dl 0
loc 52
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\Zoom\API;
6
7
use Exception;
8
use Firebase\JWT\JWT;
9
10
/**
11
 * Class JWTClient.
12
 *
13
 * @see https://marketplace.zoom.us/docs/guides/auth/jwt
14
 */
15
class JWTClient extends Client
16
{
17
    public $token;
18
19
    /**
20
     * JWTClient constructor.
21
     * Requires JWT app credentials.
22
     *
23
     * @param string $apiKey    JWT API Key
24
     * @param string $apiSecret JWT API Secret
25
     */
26
    public function __construct($apiKey, $apiSecret)
27
    {
28
        $this->token = JWT::encode(
29
            [
30
                'iss' => $apiKey,
31
                'exp' => (time() + 60) * 1000, // will expire in one minute
32
            ],
33
            $apiSecret
34
        );
35
        self::register($this);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function send($httpMethod, $relativePath, $parameters = [], $requestBody = null)
42
    {
43
        $options = [
44
            CURLOPT_CUSTOMREQUEST => $httpMethod,
45
            CURLOPT_ENCODING => '',
46
            CURLOPT_HTTPHEADER => [
47
                'authorization: Bearer '.$this->token,
48
                'content-type: application/json',
49
            ],
50
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
51
            CURLOPT_MAXREDIRS => 10,
52
            CURLOPT_RETURNTRANSFER => true,
53
            CURLOPT_TIMEOUT => 30,
54
        ];
55
        if (!is_null($requestBody)) {
56
            $jsonRequestBody = json_encode($requestBody);
57
            if (false === $jsonRequestBody) {
58
                throw new Exception('Could not generate JSON request body');
59
            }
60
            $options[CURLOPT_POSTFIELDS] = $jsonRequestBody;
61
        }
62
63
        $url = "https://api.zoom.us/v2/$relativePath";
64
        if (!empty($parameters)) {
65
            $url .= '?'.http_build_query($parameters);
66
        }
67
        $curl = curl_init($url);
68
        if (false === $curl) {
69
            throw new Exception("curl_init returned false");
70
        }
71
        curl_setopt_array($curl, $options);
72
        $responseBody = curl_exec($curl);
73
        $responseCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
74
        $curlError = curl_error($curl);
75
        curl_close($curl);
76
77
        if ($curlError) {
78
            throw new Exception("cURL Error: $curlError");
79
        }
80
81
        if (false === $responseBody || !is_string($responseBody)) {
82
            throw new Exception('cURL Error');
83
        }
84
85
        if (empty($responseCode)
86
            || $responseCode < 200
87
            || $responseCode >= 300
88
        ) {
89
            throw new Exception($responseBody, $responseCode);
90
        }
91
92
        return $responseBody;
93
    }
94
}
95