Util   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 6.78%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 52
c 2
b 0
f 0
dl 0
loc 98
rs 10
ccs 4
cts 59
cp 0.0678
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isExpired() 0 19 4
A getAuthBroker() 0 24 1
A http() 0 22 1
A logMiddleware() 0 25 2
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 GuzzleHttp\MessageFormatter;
13
use GuzzleHttp\Middleware;
14
use Illuminate\Container\Container;
15
use Illuminate\Support\Str;
16
17
class Util
18
{
19 4
    public static function isExpired(\DateTimeInterface|Carbon|string|null $expires_at): bool
20
    {
21 4
        if (is_null($expires_at)) {
22
            return false;
23
        }
24
25 4
        if ($expires_at instanceof Carbon) {
26 4
            return $expires_at->isPast();
27
        }
28
29
        $now = new \DateTime;
30
31
        if ($expires_at instanceof \DateTimeInterface) {
32
            return $now > $expires_at;
33
        }
34
35
        $expires_at = \DateTime::createFromFormat('Y-m-d H:i:s', $expires_at);
36
37
        return $now > $expires_at;
38
    }
39
40
    public static function logMiddleware(?HandlerStack $handlerStack = null): HandlerStack
41
    {
42
        $handlerStack = $handlerStack ?? HandlerStack::create();
43
44
        $id = $_SERVER['REQUEST_ID'] ?? Str::random(10);
45
46
        $messageFormats = [
47
            "HTTP_OUT_{$id} [Request] {method} {target}" => 'info',
48
            "HTTP_OUT_{$id} [Request] [Headers] \n{req_headers}" => 'debug',
49
            "HTTP_OUT_{$id} [Request] [Body] {req_body}" => 'debug',
50
            "HTTP_OUT_{$id} [Response] HTTP/{version} {code} {phrase} Size: {res_header_Content-Length}" => 'info',
51
            "HTTP_OUT_{$id} [Response] [Headers] \n{res_headers}" => 'debug',
52
            "HTTP_OUT_{$id} [Response] [Body] {res_body}" => 'debug',
53
            // "HTTP_OUT_{$id} [Error] {error}" => 'error',
54
        ];
55
56
        $logger = Container::getInstance()->get('log');
57
58
        foreach ($messageFormats as $format => $level) {
59
            $messageFormatter = new MessageFormatter($format);
60
            $logMiddleware = Middleware::log($logger, $messageFormatter, $level);
61
            $handlerStack->unshift($logMiddleware);
62
        }
63
64
        return $handlerStack;
65
    }
66
67
    public static function http(): ClientInterface
68
    {
69
        $config = Container::getInstance()->make('config');
70
71
        $handlerStack = HandlerStack::create();
72
73
        $handlerStack = Util::logMiddleware($handlerStack);
74
75
        $handlerStack->unshift(self::getAuthBroker());
76
77
        $options = array_merge([
78
            'handler' => $handlerStack,
79
            'base_uri' => $config->get('airtel-money.base_uri'),
80
            'headers' => [
81
                // 'Authorization' => 'Bearer *********',
82
                'Content-Type' => 'application/json',
83
                'X-Country' => $config->get('airtel-money.country'),
84
                'X-Currency' => $config->get('airtel-money.currency'),
85
            ],
86
        ], (array) $config->get('airtel-money.guzzle.options'));
87
88
        return new Client($options);
89
    }
90
91
    public static function getAuthBroker(): OAuth2Middleware
92
    {
93
        $config = Container::getInstance()->make('config');
94
95
        $handlerStack = Util::logMiddleware();
96
97
        $options = array_merge([
98
            'base_uri' => $config->get('airtel-money.base_uri'),
99
            'handler' => $handlerStack,
100
        ], $config->get('airtel-money.guzzle.options'));
101
102
        $client = new Client($options);
103
104
        $config = [
105
            'client_id' => $config->get('airtel-money.client_id'),
106
            'client_secret' => $config->get('airtel-money.client_secret'),
107
            'token_uri' => $config->get('airtel-money.token_uri'),
108
        ];
109
110
        $clientCredGrant = new ClientCredentials($client, $config);
111
112
        $tokenRepo = new TokenRepository;
113
114
        return new OAuth2Middleware($clientCredGrant, null, $tokenRepo);
115
    }
116
}
117