Simulate   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 6.78 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 8
loc 118
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 8 8 2
A from() 0 8 2
A usingReference() 0 5 1
A setCommand() 0 8 2
B push() 0 20 7

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 Samerior\MobileMoney\Mpesa\Library;
4
5
use Samerior\MobileMoney\Mpesa\Exceptions\MpesaException;
6
use GuzzleHttp\Exception\ClientException;
7
8
/**
9
 * Class Simulate
10
 * @package Samerior\MobileMoney\Mpesa\Library
11
 */
12
class Simulate extends ApiCore
13
{
14
    /**
15
     * @var string
16
     */
17
    private $amount;
18
    /**
19
     * @var int
20
     */
21
    private $number;
22
    /**
23
     * @var string
24
     */
25
    private $command = 'CustomerPayBillOnline';
26
    //CustomerBuyGoodsOnline
27
    /**
28
     * @var string
29
     */
30
    private $reference = 'Testing';
31
32
    /**
33
     * Set the request amount to be deducted.
34
     *
35
     * @param int $amount
36
     *
37
     * @return $this
38
     * @throws \Exception
39
     * @throws MpesaException
40
     */
41 View Code Duplication
    public function request($amount): self
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...
42
    {
43
        if (!\is_numeric($amount)) {
44
            throw new MpesaException('The amount must be numeric');
45
        }
46
        $this->amount = $amount;
0 ignored issues
show
Documentation Bug introduced by
It seems like $amount can also be of type integer or double. However, the property $amount is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
47
        return $this;
48
    }
49
50
    /**
51
     * Set the Mobile Subscriber Number to deduct the amount from.
52
     * Must be in format 2547XXXXXXXX.
53
     *
54
     * @param string $number
55
     *
56
     * @return $this
57
     * @throws MpesaException
58
     */
59
    public function from($number): self
60
    {
61
        if (!starts_with($number, '2547')) {
62
            throw new MpesaException('The subscriber number must start with 2547');
63
        }
64
        $this->number = $number;
0 ignored issues
show
Documentation Bug introduced by
The property $number was declared of type integer, but $number is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
65
        return $this;
66
    }
67
68
    /**
69
     * Set the product reference number to bill the account.
70
     *
71
     * @param int $reference
72
     *
73
     * @return $this
74
     */
75
    public function usingReference($reference): self
76
    {
77
        $this->reference = $reference;
0 ignored issues
show
Documentation Bug introduced by
The property $reference was declared of type string, but $reference is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
78
        return $this;
79
    }
80
81
    /**
82
     * Set the unique command for this transaction type.
83
     *
84
     * @param string $command
85
     *
86
     * @return $this
87
     * @throws MpesaException
88
     */
89
    public function setCommand($command): self
90
    {
91
        if (!\in_array($command, self::VALID_COMMANDS, true)) {
92
            throw new MpesaException('Invalid command sent');
93
        }
94
        $this->command = $command;
95
        return $this;
96
    }
97
98
    /**
99
     * Prepare the transaction simulation request
100
     *
101
     * @param int $amount
102
     * @param int $number
103
     * @param string $reference
104
     * @param string $command
105
     * @return mixed
106
     * @throws MpesaException
107
     * @throws \GuzzleHttp\Exception\GuzzleException
108
     */
109
    public function push($number = null, $amount = null, $reference = null, $command = null)
110
    {
111
        if (!\config('samerior.mpesa.sandbox', true)) {
112
            throw new MpesaException('Cannot simulate a transaction in the live environment.');
113
        }
114
        $shortCode = \config('samerior.mpesa.c2b.short_code');
115
        $good_phone = $this->formatPhoneNumber($number ?: $this->number);
116
        $body = [
117
            'CommandID' => $command ?: $this->command,
118
            'Amount' => $amount ?: $this->amount,
119
            'Msisdn' => $good_phone,
120
            'ShortCode' => $shortCode,
121
            'BillRefNumber' => $reference ?: $this->reference,
122
        ];
123
        try {
124
            return $this->sendRequest($body, 'simulate');
125
        } catch (ClientException $exception) {
126
            return \json_decode($exception->getResponse()->getBody());
127
        }
128
    }
129
}
130