RegisterUrl   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 96
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
A onValidation() 0 5 1
A onConfirmation() 0 5 1
A onTimeout() 0 8 3
B submit() 0 13 8
1
<?php
2
3
namespace Samerior\MobileMoney\Mpesa\Library;
4
5
use Samerior\MobileMoney\Mpesa\Exceptions\MpesaException;
6
7
/**
8
 * Class RegisterUrl
9
 * @package Samerior\MobileMoney\Mpesa\Library
10
 */
11
class RegisterUrl extends ApiCore
12
{
13
    /**
14
     * The short code to register callbacks for.
15
     *
16
     * @var string
17
     */
18
    protected $shortCode;
19
    /**
20
     * The validation callback.
21
     *
22
     * @var
23
     */
24
    protected $validationURL;
25
    /**
26
     * The confirmation callback.
27
     *
28
     * @var
29
     */
30
    protected $confirmationURL;
31
    /**
32
     * The status of the request in case a timeout occurs.
33
     *
34
     * @var string
35
     */
36
    protected $onTimeout = 'Completed';
37
38
    /**
39
     * @param $shortCode
40
     * @return $this
41
     */
42
    public function register($shortCode)
43
    {
44
        $this->shortCode = $shortCode;
45
        return $this;
46
    }
47
48
    /**
49
     * @param string $validationURL
50
     * @return $this
51
     */
52
    public function onValidation($validationURL)
53
    {
54
        $this->validationURL = $validationURL;
55
        return $this;
56
    }
57
58
    /**
59
     * @param $confirmationURL
60
     * @return $this
61
     */
62
    public function onConfirmation($confirmationURL)
63
    {
64
        $this->confirmationURL = $confirmationURL;
65
        return $this;
66
    }
67
68
    /**
69
     * @param string $onTimeout
70
     * @return $this
71
     * @throws \Exception
72
     * @throws MpesaException
73
     */
74
    public function onTimeout($onTimeout = 'Completed')
75
    {
76
        if ($onTimeout !== 'Completed' && $onTimeout !== 'Cancelled') {
77
            throw new MpesaException('Invalid timeout argument. Use Completed or Cancelled');
78
        }
79
        $this->onTimeout = $onTimeout;
80
        return $this;
81
    }
82
83
    /**
84
     * @param string|null $shortCode
85
     * @param string|null $confirmationURL
86
     * @param string|null $validationURL
87
     * @param string|null $onTimeout
88
     * @return mixed
89
     * @throws MpesaException
90
     * @throws \Exception
91
     * @throws \GuzzleHttp\Exception\GuzzleException
92
     */
93
    public function submit($shortCode = null, $confirmationURL = null, $validationURL = null, $onTimeout = null)
94
    {
95
        if ($onTimeout && $onTimeout !== 'Completed' && $onTimeout = 'Cancelled') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $onTimeout of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
96
            throw new MpesaException('Invalid timeout argument. Use Completed or Cancelled');
97
        }
98
        $body = [
99
            'ShortCode' => $shortCode ?: $this->shortCode,
100
            'ResponseType' => $onTimeout ?: $this->onTimeout,
101
            'ConfirmationURL' => $confirmationURL ?: $this->confirmationURL,
102
            'ValidationURL' => $validationURL ?: $this->validationURL
103
        ];
104
        return $this->sendRequest($body, 'register');
105
    }
106
}
107