StkRequest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 135
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setPayBill() 0 5 1
A request() 0 8 2
A formatPhoneNumber() 0 10 3
A from() 0 9 2
A usingReferenceId() 0 5 1
A transact() 0 4 1
A status() 0 9 2
1
<?php
2
3
namespace Samerior\MobileMoney\Equity\Library;
4
5
/**
6
 * Class StkRequest
7
 * @package Samerior\MobileMoney\Equity\Library
8
 */
9
class StkRequest
10
{
11
    /**
12
     * The amount to be deducted.
13
     *
14
     * @var int
15
     */
16
    protected $amount;
17
    /**
18
     * The Mobile Subscriber Number.
19
     *
20
     * @var int
21
     */
22
    protected $number;
23
    /**
24
     * The product reference identifier.
25
     *
26
     * @var int
27
     */
28
    protected $referenceId;
29
    /**
30
     * The transaction handler.
31
     *
32
     * @var Transactor
33
     */
34
    private $transactor;
35
36
    /**
37
     * Cashier constructor.
38
     *
39
     * @param Transactor $transactor
40
     */
41
    public function __construct(Transactor $transactor)
42
    {
43
        $this->transactor = $transactor;
44
    }
45
46
    /**
47
     * Override the config pay bill number and pass key.
48
     *
49
     * @param $payBillNumber
50
     * @param $payBillPassKey
51
     *
52
     * @return $this
53
     */
54
    public function setPayBill($payBillNumber, $payBillPassKey)
55
    {
56
        $this->transactor->setPayBill($payBillNumber, $payBillPassKey);
57
        return $this;
58
    }
59
60
    /**
61
     * Set the request amount to be deducted.
62
     *
63
     * @param int $amount
64
     *
65
     * @return $this
66
     * @throws \InvalidArgumentException
67
     */
68
    public function request($amount)
69
    {
70
        if (!is_numeric($amount)) {
71
            throw new \InvalidArgumentException('The amount must be numeric');
72
        }
73
        $this->amount = $amount;
0 ignored issues
show
Documentation Bug introduced by
It seems like $amount can also be of type double or string. However, the property $amount is declared as type integer. 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...
74
        return $this;
75
    }
76
77
    private function formatPhoneNumber(&$number)
78
    {
79
        if (strlen($number) === 10) {
80
            $needle = '07';
81
            if (starts_with($number, $needle)) {
82
                $pos = strpos($number, $needle);
83
                $number = substr_replace($number, '2547', $pos, strlen($needle));
84
            }
85
        }
86
    }
87
88
    /**
89
     * Set the Mobile Subscriber Number to deduct the amount from.
90
     * Must be in format 2547XXXXXXXX.
91
     *
92
     * @param int $number
93
     *
94
     * @return $this
95
     * @throws \InvalidArgumentException
96
     */
97
    public function from($number)
98
    {
99
        $this->formatPhoneNumber($number);
100
        if (!starts_with($number, '2547')) {
101
            throw new \InvalidArgumentException('The subscriber number must start with 2547');
102
        }
103
        $this->number = $number;
104
        return $this;
105
    }
106
107
    /**
108
     * Set the product reference number to bill the account.
109
     *
110
     * @param int $referenceId
111
     *
112
     * @return $this
113
     */
114
    public function usingReferenceId($referenceId)
115
    {
116
        $this->referenceId = $referenceId;
117
        return $this;
118
    }
119
120
    /**
121
     * Initiate the transaction
122
     *
123
     * @return mixed|\Psr\Http\Message\ResponseInterface
124
     */
125
    public function transact()
126
    {
127
        return $this->transactor->process($this->amount, $this->number, $this->referenceId);
128
    }
129
130
    /**
131
     * Get payment status
132
     * @return mixed
133
     */
134
    public function status()
135
    {
136
        $x = null;
137
        if (is_object($x)) {
138
            $extra = ['gateway' => $request->mode,
0 ignored issues
show
Bug introduced by
The variable $request does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
139
                'description' => 'Payment of ' . $request->to_pay . ' received. Ref ' . $x->result->transactionRef];
140
            $this->credit($request->user()->id, $request->to_pay, $extra);
0 ignored issues
show
Bug introduced by
The method credit() does not seem to exist on object<Samerior\MobileMo...ity\Library\StkRequest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
        }
142
    }
143
}
144