VTPass   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 186
Duplicated Lines 45.7 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 19.76%

Importance

Changes 0
Metric Value
dl 85
loc 186
ccs 17
cts 86
cp 0.1976
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A buyAirtime() 0 32 4
A buyData() 0 4 1
A queryOrder() 28 28 4
A variationCodes() 28 28 4
A verifySmartCard() 29 29 4
A payUtility() 0 31 4

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
4
namespace Djunehor\Vtu\Concrete;
5
6
7
use Djunehor\Vtu\Contracts\VtuServiceInterface;
8
use GuzzleHttp\Exception\ClientException;
9
use GuzzleHttp\Psr7\Request;
10
use Illuminate\Support\Str;
11
12
class VTPass extends Vtu
13
{
14
    private $sandboxURL='https://sandbox.vtpass.com/api/';
15
    private $liveAPI='https://vtpass.com/api/';
16
    private $baseUrl;
17
18 1
    public function __construct($username = null, $password = null, $live = true)
19
    {
20 1
        $this->username = $username ?? config('laravel-vtu.vtpass.username');
21 1
        $this->password = $password ?? config('laravel-vtu.vtpass.password');
22 1
        $this->baseUrl = $live ? $this->liveAPI : $this->sandboxURL;
23
24 1
        $this->client = $this->getInstance();
25
26 1
    }
27
28
    /**
29
     * acceptable network: mtn, airtel, etisalat, glo
30
     * @param $amount
31
     * @param $mobileNumber
32
     * @param $mobileNetwork
33
     * @param null $callBackUrl
34
     * @return bool
35
     */
36 1
    public function buyAirtime($amount, $mobileNumber, $mobileNetwork, $callBackUrl = null): bool
37
    {
38
        $headers = [
39 1
            "Authorization" => "Basic ".$this->username.$this->password
40
        ];
41 1
        $this->request = new Request('POST', $this->baseUrl."pay", $headers);
42
43
        try {
44 1
            $response = $this->client->send($this->request, [
45
                'form_params' => [
46 1
                    'request_id' => $requestId = Str::random(32),
47 1
                    'serviceID' => $mobileNetwork,
48 1
                    'amount' => $amount,
49 1
                    'phone' => $mobileNumber,
50
                ],
51
            ]);
52
53
            $response = json_decode($response->getBody()->getContents(), true);
54
            $this->response = $response['response_description'];
55
            $this->orderId = $requestId;
0 ignored issues
show
Bug introduced by
The property orderId does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
57
            return $response['code'] == '000' ? true : false;
58 1
        } catch (ClientException $e) {
59 1
            $this->httpError = $e;
60
61 1
            return false;
62
        } catch (\Exception $e) {
63
            $this->httpError = $e;
64
65
            return false;
66
        }
67
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72
    public function buyData($mobileNumber, $mobileNetwork, $dataPlan, $callBackUrl): bool
73
    {
74
        // TODO: Implement buyData() method.
75
    }
76
77
78 View Code Duplication
    public function queryOrder($orderId): bool
0 ignored issues
show
Unused Code introduced by
The parameter $orderId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
79
    {
80
        $headers = [
81
            "Authorization" => "Basic ".$this->username.$this->password
82
        ];
83
        $this->request = new Request('POST', $this->baseUrl."pay", $headers);
84
85
        try {
86
            $response = $this->client->send($this->request, [
87
                'form_params' => [
88
                    'request_id' => $requestId = Str::random(32),
89
                ],
90
            ]);
91
92
            $response = json_decode($response->getBody()->getContents(), true);
93
            $this->response = $response['content'];
94
95
            return $response['code'] == '001' ? true : false;
96
        } catch (ClientException $e) {
97
            $this->httpError = $e;
98
99
            return false;
100
        } catch (\Exception $e) {
101
            $this->httpError = $e;
102
103
            return false;
104
        }
105
    }
106
107 View Code Duplication
    public function variationCodes($serviceId): 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...
108
    {
109
        $headers = [
110
            "Authorization" => "Basic ".$this->username.$this->password
111
        ];
112
        $this->request = new Request('GET', $this->baseUrl."service-variations", $headers);
113
114
        try {
115
            $response = $this->client->send($this->request, [
116
                'query_params' => [
117
                    'serviceID' => $serviceId,
118
                ],
119
            ]);
120
121
            $response = json_decode($response->getBody()->getContents(), true);
122
            $this->response = $response['content'];
123
124
            return $response['content'] ? true : false;
125
        } catch (ClientException $e) {
126
            $this->httpError = $e;
127
128
            return false;
129
        } catch (\Exception $e) {
130
            $this->httpError = $e;
131
132
            return false;
133
        }
134
    }
135
136 View Code Duplication
    public function verifySmartCard($smartCardNumber, $serviceId): 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...
137
    {
138
        $headers = [
139
            "Authorization" => "Basic ".$this->username.$this->password
140
        ];
141
        $this->request = new Request('GET', $this->baseUrl."merchant-verify", $headers);
142
143
        try {
144
            $response = $this->client->send($this->request, [
145
                'query_params' => [
146
                    'billersCode' => $smartCardNumber,
147
                    'serviceID' => $serviceId,
148
                ],
149
            ]);
150
151
            $response = json_decode($response->getBody()->getContents(), true);
152
            $this->response = $response['content'];
153
154
            return $response['code'] == '000' ? true : false;
155
        } catch (ClientException $e) {
156
            $this->httpError = $e;
157
158
            return false;
159
        } catch (\Exception $e) {
160
            $this->httpError = $e;
161
162
            return false;
163
        }
164
    }
165
166
    public function payUtility($smartCardNumber, $cableTv, $package, $callbackUrl = null, $phone = null): bool
167
    {
168
        $headers = [
169
            "Authorization" => "Basic ".$this->username.$this->password
170
        ];
171
        $this->request = new Request('GET', $this->baseUrl."pay", $headers);
172
173
        try {
174
            $response = $this->client->send($this->request, [
175
                'query_params' => [
176
                    'billersCode' => $smartCardNumber,
177
                    'serviceID' => $cableTv,
178
                    'variation_code' => $package,
179
                    'phone' => $phone,
180
                ],
181
            ]);
182
183
            $response = json_decode($response->getBody()->getContents(), true);
184
            $this->response = $response['content'];
185
186
            return $response['code'] == '000' ? true : false;
187
        } catch (ClientException $e) {
188
            $this->httpError = $e;
189
190
            return false;
191
        } catch (\Exception $e) {
192
            $this->httpError = $e;
193
194
            return false;
195
        }
196
    }
197
}