Completed
Pull Request — master (#355)
by
unknown
02:53
created

Connection::refreshTokens()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Picqer\Laravel\Exact;
4
5
use GuzzleHttp\Psr7\Response;
6
use Illuminate\Support\Facades\Config;
7
use Picqer\Financials\Exact\Connection as BaseConnection;
8
9
class Connection extends BaseConnection
10
{
11
    
12
    /**
13
     * @var \Picqer\Laravel\Exact\DefaultProfile
14
     */
15
    protected $token_profile;
16
    
17
    /**
18
     * @throws \Exception
19
     */
20
    protected function beforeValidatingAccessToken()
21
    {
22
        parent::beforeValidatingAccessToken();
23
    
24
        $current_refresh_token = $this->getRefreshToken();
25
        $latest_refresh_token = $this->getProfile()
26
            ->getLatestRefreshToken($current_refresh_token);
27
    
28
        if($latest_refresh_token !== $current_refresh_token) {
29
            $this->useNewRefreshToken($latest_refresh_token);
30
        }
31
    }
32
    
33
    /**
34
     * @return bool
35
     * @throws \Exception
36
     */
37
    protected function minuteLimitExceeded()
38
    {
39
        return $this->getProfile()->getRemainingMinuteLimit() <= $this->callsLimit * 0.10;
40
    }
41
    
42
    /**
43
     * @param \GuzzleHttp\Psr7\Response $response
44
     *
45
     * @throws \Exception
46
     */
47
    protected function handleRateLimitsFromResponse(Response $response)
48
    {
49
        if($response->hasHeader('X-RateLimit-Minutely-Limit') === false) {
50
            return;
51
        }
52
        
53
        $this->callsLimit = (int) $response->getHeaderLine('X-RateLimit-Minutely-Limit');
54
        $this->callsLeft = (int) $response->getHeaderLine('X-RateLimit-Minutely-Remaining');
55
        $this->responseTimestamp = strtotime($response->getHeaderLine('Date'));
56
        
57
        $this->getProfile()->handleMinuteLimit($this->callsLeft);
58
    }
59
    
60
    /**
61
     * @throws \Exception
62
     */
63
    protected function refreshTokens()
64
    {
65
        $this->getProfile()->handleNewTokens(
66
            $this->getResponseTimestamp(),
67
            $this->getRefreshToken(),
68
            $this->getAccessToken()
69
        );
70
    }
71
    
72
    /**
73
     * @throws \Exception
74
     *
75
     * @return \Picqer\Laravel\Exact\DefaultProfile
76
     */
77
    protected function getProfile()
78
    {
79
        $profile_class_name = Config::get('exact.profile', DefaultProfile::class);
80
        
81
        $profile = new $profile_class_name();
82
        if($profile instanceof DefaultProfile === false) {
83
            throw new \Exception('Profile must extend \Picqer\Financials\Exact\DefaultProfile');
84
        }
85
        
86
        return $this->token_profile = $profile;
87
    }
88
    
89
    /**
90
     * @param $token
91
     *
92
     * @throws \Picqer\Financials\Exact\ApiException
93
     */
94
    protected function useNewRefreshToken($token)
95
    {
96
        $this->setRefreshToken($token);
97
    
98
        // set access token to empty string so a new one will be generated..
99
        $this->setAccessToken('');
100
        $this->acquireAccessToken();
101
    }
102
    
103
}