Connect::setClient()   A
last analyzed

Complexity

Conditions 2
Paths 4

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 10
rs 10
1
<?php
2
/**
3
 * Class Connect
4
 * @package Wesleydeveloper\Hotmart
5
 * @author Wesley Silva <[email protected]>
6
 */
7
8
9
namespace Wesleydeveloper\Hotmart\Support;
10
11
use GuzzleHttp\Client;
12
use InvalidArgumentException;
13
14
class Connect
15
{
16
    private const ENDPOINT = 'https://api-sec-vlc.hotmart.com/';
17
    private $config;
18
19
    public function __construct($config = null)
20
    {
21
        if(is_array($config)){
22
            extract($config);
23
            $this->setConfig($client_id, $client_secret, $basic);
24
        }
25
    }
26
27
    /**
28
     * @param string $client_id
29
     * @param string $client_secret
30
     * @param string $basic
31
     */
32
    public function setConfig($client_id, $client_secret, $basic)
33
    {
34
        $this->config = new Config($client_id, $client_secret, $basic);
35
        $this->config->setData([
36
            'form_params' => [
37
                'grant_type' => 'client_credentials',
38
                'client_id' => $this->config->getClientId(),
39
                'client_secret' => $this->config->getClientSecret()
40
            ],
41
            'headers' => ['Authorization' => "Basic {$this->config->getBasic()}"]
42
        ]);
43
        $this->setClient();
44
    }
45
46
    /**
47
     * @return mixed
48
     */
49
    public function getConfig()
50
    {
51
        return $this->config;
52
    }
53
54
    //Method: POST
55
    // URI: /security/oauth/token?grant_type=client_credentials&client_id=[CLIENT_ID]&client_secret=[CLIENT_SECRET]
56
    //Header: "Authorization" -> "[BASIC]"
57
58
    public function setClient(){
59
        $client = new Client([
60
            'base_uri' => self::ENDPOINT,
61
        ]);
62
        try {
63
            $request = $client->post('/security/oauth/token', $this->getConfig()->getData());
64
            $response = json_decode($request->getBody()->getContents(), true);
65
            $this->getConfig()->setAccessToken($response['access_token']);
66
        } catch (\Exception $e) {
67
            return $e;
68
        }
69
    }
70
71
}