Passed
Push — main ( 9589c4...cbe1f8 )
by Brian
02:39
created

Util::isExpired()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 7.456

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 19
rs 9.9666
ccs 4
cts 10
cp 0.4
crap 7.456
1
<?php
2
3
namespace Bmatovu\AirtelMoney\Support;
4
5
use Bmatovu\AirtelMoney\Auth\GrantTypes\ClientCredentials;
6
use Bmatovu\AirtelMoney\Auth\OAuth2Middleware;
7
use Bmatovu\AirtelMoney\Auth\Repositories\TokenRepository;
8
use Carbon\Carbon;
9
use GuzzleHttp\Client;
10
use GuzzleHttp\ClientInterface;
11
use GuzzleHttp\HandlerStack;
12
use Illuminate\Container\Container;
13
14
class Util
15
{
16 4
    public static function isExpired(\DateTimeInterface|Carbon|string|null $expires_at): bool
17
    {
18 4
        if (is_null($expires_at)) {
19
            return false;
20
        }
21
22 4
        if ($expires_at instanceof Carbon) {
23 4
            return $expires_at->isPast();
24
        }
25
26
        $now = new \DateTime;
27
28
        if ($expires_at instanceof \DateTimeInterface) {
29
            return $now > $expires_at;
30
        }
31
32
        $expires_at = \DateTime::createFromFormat('Y-m-d H:i:s', $expires_at);
33
34
        return $now > $expires_at;
35
    }
36
37
    public static function http(): ClientInterface
38
    {
39
        $config = Container::getInstance()->make('config');
40
41
        $handlerStack = HandlerStack::create();
42
43
        $handlerStack->push(new GuzzleHttpLogMiddleware);
44
45
        $handlerStack->unshift(self::getAuthBroker());
46
47
        $options = array_merge([
48
            'handler' => $handlerStack,
49
            'base_uri' => $config->get('airtel-money.base_uri'),
50
            'headers' => [
51
                // 'Authorization' => 'Bearer *********',
52
                'Content-Type' => 'application/json',
53
                'X-Country' => $config->get('airtel-money.country'),
54
                'X-Currency' => $config->get('airtel-money.currency'),
55
            ],
56
        ], (array) $config->get('airtel-money.guzzle.options'));
57
58
        return new Client($options);
59
    }
60
61
    public static function getAuthBroker(): OAuth2Middleware
62
    {
63
        $config = Container::getInstance()->make('config');
64
65
        $handlerStack = HandlerStack::create();
66
67
        $handlerStack->push(new GuzzleHttpLogMiddleware);
68
69
        $options = array_merge([
70
            'base_uri' => $config->get('airtel-money.base_uri'),
71
            'handler' => $handlerStack,
72
        ], $config->get('airtel-money.guzzle.options'));
73
74
        $client = new Client($options);
75
76
        $config = [
77
            'client_id' => $config->get('airtel-money.client_id'),
78
            'client_secret' => $config->get('airtel-money.client_secret'),
79
            'token_uri' => $config->get('airtel-money.authorization.token_uri'),
80
        ];
81
82
        $clientCredGrant = new ClientCredentials($client, $config);
83
84
        $tokenRepo = new TokenRepository;
85
86
        return new OAuth2Middleware($clientCredGrant, null, $tokenRepo);
87
    }
88
}
89