RestClient::tokenize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 16
ccs 0
cts 13
cp 0
crap 2
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This is the class utility responsible to wrap REST API request management and
5
 * interact with needed services.
6
 */
7
8
namespace Cdf\BiCoreBundle\Utils\Api;
9
10
use GuzzleHttp\Client;
11
12
class RestClient
13
{
14
15
16
    //parameters
17
    private string $endpoint;
18
    private string $clientkey;
19
20
    public function __construct(string $endpoint, string $clientkey)
21
    {
22
        $this->endpoint = $endpoint;
23
        $this->clientkey = $clientkey;
24
    }
25
26
    private function tokenize(): mixed
27
    {
28
29
        $body = array();
30
        $body['grant_type'] = 'client_credentials';
31
32
        $client = new Client();
33
        $res = $client->request('POST', $this->endpoint, [
34
            'headers' => [
35
                'Content-Type' => 'application/x-www-form-urlencoded',
36
                'Accept' => '*/*',
37
                'Authorization' => 'Basic '.$this->clientkey,
38
            ],
39
            'form_params' => ['grant_type' => 'client_credentials'],
40
        ]);
41
        return $res;
42
    }
43
44
    /**
45
     * Perform a call to the principal method of token acquisition
46
     */
47
    public function oauth2Principal(): mixed
48
    {
49
        $response = $this->tokenize();
50
        $content = $response->getBody();
51
        $data = json_decode($content);
52
        return $data;
53
    }
54
}
55