CowrieSys   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 73.27 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 43.64%

Importance

Changes 0
Metric Value
dl 74
loc 101
ccs 24
cts 55
cp 0.4364
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A sign() 0 3 1
A buyAirtime() 37 37 4
A buyData() 37 37 4
A payUtility() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Djunehor\Vtu\Concrete;
4
5
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Psr7\Request;
8
9
class CowrieSys extends Vtu
10
{
11
    private $baseUrl ='https://api.cowriesys.com';
12
13 1
    public function __construct($clientId = null, $clientKey = null)
14
    {
15 1
        $this->username = $clientId ?? config('laravel-vtu.cowriesys.client_id');
16 1
        $this->password = $clientKey ?? config('laravel-vtu.cowriesys.client_key');
17
18 1
        $this->client = $this->getInstance();
19
20 1
    }
21
22 1
    function sign($message) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23 1
        return base64_encode(hash_hmac('sha256', $message, base64_decode($this->password), true));
24
    }
25
26 1 View Code Duplication
    public function buyAirtime($amount, $mobileNumber, $mobileNetwork, $callbackUrl = null) : bool {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27 1
        $nonce = uniqid();
28 1
        $queryString = '?net='.$mobileNetwork.'&msisdn='.$mobileNumber.'&amount='.$amount.'&xref='.$nonce;
29 1
        $signature = $this->sign($nonce.$queryString);
30
        $headers = [
31 1
            'ClientId: '.$this->username,
32 1
            'Signature: '.$signature,
33 1
            'Nonce: '.$nonce
34
        ];
35 1
        $api = '/airtime/Credit';
36 1
        $this->request = new Request('GET', $this->baseUrl.$api, $headers);
37
38
39
        try {
40 1
            $response = $this->client->send($this->request, [
41
                'query' => [
42 1
                    'net' => $mobileNetwork,
43 1
                    'msisdn' => $mobileNumber,
44 1
                    'amount' => $amount,
45 1
                    'xref' => $nonce,
46
                ],
47
            ]);
48
49
            $response = json_decode($response->getBody()->getContents(), true);
50
            $this->response = $response;
51
52
            return $response ? true : false;
53 1
        } catch (ClientException $e) {
54 1
            $this->httpError = $e;
55
56 1
            return false;
57
        } catch (\Exception $e) {
58
            $this->httpError = $e;
59
60
            return false;
61
        }
62
    }
63
64 View Code Duplication
    public function buyData($amount, $mobileNumber, $mobileNetwork, $callbackUrl = null) : bool {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
        $nonce = uniqid();
66
        $queryString = '?net='.$mobileNetwork.'&msisdn='.$mobileNumber.'&amount='.$amount.'&xref='.$nonce;
67
        $signature = $this->sign($nonce.$queryString);
68
        $headers = [
69
            'ClientId: '.$this->username,
70
            'Signature: '.$signature,
71
            'Nonce: '.$nonce
72
        ];
73
        $api = '/data/Credit';
74
        $this->request = new Request('GET', $this->baseUrl.$api, $headers);
75
76
77
        try {
78
            $response = $this->client->send($this->request, [
79
                'query' => [
80
                    'net' => $mobileNetwork,
81
                    'msisdn' => $mobileNumber,
82
                    'amount' => $amount,
83
                    'xref' => $nonce,
84
                ],
85
            ]);
86
87
            $response = json_decode($response->getBody()->getContents(), true);
88
            $this->response = $response;
89
90
            return $response ? true : false;
91
        } catch (ClientException $e) {
92
            $this->httpError = $e;
93
94
            return false;
95
        } catch (\Exception $e) {
96
            $this->httpError = $e;
97
98
            return false;
99
        }
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    public function payUtility($smartCardNumber, $cableTv, $package, $callBackUrl): bool
106
    {
107
        return false;
108
    }
109
}