Passed
Pull Request — master (#190)
by Arman
03:14
created

CloudAppTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 43
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A sendRequest() 0 34 3
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Libraries\Storage\Traits;
16
17
use Quantum\Http\Exceptions\HttpException;
18
use Quantum\Exceptions\BaseException;
19
use Exception;
20
21
/**
22
 * Trait CloudAppTrait
23
 * @package Quantum\Libraries\Storage
24
 */
25
trait CloudAppTrait
26
{
27
28
    /**
29
     * @inheritDoc
30
     * @throws BaseException
31
     * @throws HttpException
32
     * @throws Exception
33
     */
34
    public function sendRequest(string $uri, $data = null, array $headers = [], string $method = 'POST')
35
    {
36
        $this->httpClient
37
            ->createRequest($uri)
38
            ->setMethod($method)
39
            ->setData($data)
40
            ->setHeaders($headers)
41
            ->start();
42
43
        $errors = $this->httpClient->getErrors();
44
        $responseBody = $this->httpClient->getResponseBody();
45
46
        if ($errors) {
47
            $code = $errors['code'];
48
49
            if ($this->accessTokenNeedsRefresh($code, $responseBody)) {
0 ignored issues
show
Bug introduced by
It seems like accessTokenNeedsRefresh() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
            if ($this->/** @scrutinizer ignore-call */ accessTokenNeedsRefresh($code, $responseBody)) {
Loading history...
50
                $prevUrl = $this->httpClient->url();
51
                $prevData = $this->httpClient->getData();
52
                $prevHeaders = $this->httpClient->getRequestHeaders();
53
54
                $refreshToken = $this->tokenService->getRefreshToken();
55
56
                $accessToken = $this->fetchAccessTokenByRefreshToken($refreshToken);
0 ignored issues
show
Bug introduced by
It seems like fetchAccessTokenByRefreshToken() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
                /** @scrutinizer ignore-call */ 
57
                $accessToken = $this->fetchAccessTokenByRefreshToken($refreshToken);
Loading history...
57
58
                $prevHeaders['Authorization'] = 'Bearer ' . $accessToken;
59
60
                $responseBody = $this->sendRequest($prevUrl, $prevData, $prevHeaders);
61
62
            } else {
63
                throw new Exception(json_encode($responseBody ?? $errors), E_ERROR);
64
            }
65
        }
66
67
        return $responseBody;
68
    }
69
}