Passed
Pull Request — master (#238)
by
unknown
07:49
created

Invoice   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 33
c 4
b 0
f 0
dl 0
loc 174
rs 10
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A amount() 0 8 2
A getUuid() 0 6 2
A getNumericUuid() 0 9 2
A getDriver() 0 3 1
A __construct() 0 3 1
A convertUuidToNumeric() 0 3 1
A uuid() 0 10 3
A getTransactionId() 0 3 1
A via() 0 5 1
A getAmount() 0 3 1
A transactionId() 0 5 1
1
<?php
2
3
namespace Shetabit\Multipay;
4
5
use Ramsey\Uuid\Uuid;
6
use Shetabit\Multipay\Traits\HasDetail;
7
8
class Invoice
9
{
10
    use HasDetail;
11
12
    /**
13
     * invoice's unique universal id (uuid)
14
     *
15
     * @var string
16
     */
17
    protected $uuid;
18
19
    /**
20
     * Amount
21
     *
22
     * @var int|float
23
     */
24
    protected $amount = 0;
25
26
    /**
27
     * invoice's transaction id
28
     *
29
     * @var string
30
     */
31
    protected $transactionId;
32
33
    /**
34
     * @var string
35
     */
36
    protected $driver;
37
38
    protected $uuidIsProvidedByUser = false;
39
40
    /**
41
     * Invoice constructor.
42
     *
43
     * @throws \Exception
44
     */
45
    public function __construct()
46
    {
47
        $this->uuid();
48
    }
49
50
    /**
51
     * Set invoice uuid. this method wouldn't allow to modify uuid if it's provided by user.
52
     *
53
     * @param $uuid|null
54
     * @return self
55
     */
56
    public function uuid($uuid = null)
57
    {
58
        if (empty($this->uuid)) {
59
            $this->uuidIsProvidedByUser = true;
60
            $this->uuid = $uuid;
61
        } elseif (!$this->uuidIsProvidedByUser) {
62
            $this->uuid = $uuid;
63
        }
64
65
        return $this;
66
    }
67
68
    /**
69
     * Get invoice uuid
70
     *
71
     * @return string
72
     */
73
    public function getUuid()
74
    {
75
        if (empty($this->uuid)) {
76
            $this->uuid = Uuid::uuid4()->toString();
77
        }
78
        return $this->uuid;
79
    }
80
81
    /**
82
     * Convert invoice uuid to numeric when needed and get it.
83
     *
84
     * @return string
85
     */
86
    public function getNumericUuid()
87
    {
88
        if (is_numeric($this->uuid)) {
89
            return $this->uuid;
90
        }
91
92
        $this->convertUuidToNumeric();
93
94
        return $this->uuid;
95
    }
96
97
98
    /**
99
     * Convert uuid value to numeric if its not provided by user.
100
     * @return self
101
     */
102
    public function convertUuidToNumeric()
103
    {
104
        return $this->uuid(crc32($this->getUuid()));
105
    }
106
107
    /**
108
     * Set the amount of invoice
109
     *
110
     * @param $amount
111
     *
112
     * @return $this
113
     *
114
     * @throws \Exception
115
     */
116
    public function amount($amount)
117
    {
118
        if (!is_numeric($amount)) {
119
            throw new \Exception('Amount value should be a number (integer or float).');
120
        }
121
        $this->amount = $amount;
122
123
        return $this;
124
    }
125
126
    /**
127
     * Get the value of invoice
128
     *
129
     * @return int|float
130
     */
131
    public function getAmount()
132
    {
133
        return $this->amount;
134
    }
135
136
    /**
137
     * set transaction id
138
     *
139
     * @param $id
140
     *
141
     * @return $this
142
     */
143
    public function transactionId($id)
144
    {
145
        $this->transactionId = $id;
146
147
        return $this;
148
    }
149
150
    /**
151
     * Get the value of transaction's id
152
     *
153
     * @return string
154
     */
155
    public function getTransactionId()
156
    {
157
        return $this->transactionId;
158
    }
159
160
    /**
161
     * Set the value of driver
162
     *
163
     * @param $driver
164
     *
165
     * @return $this
166
     */
167
    public function via($driver)
168
    {
169
        $this->driver = $driver;
170
171
        return $this;
172
    }
173
174
    /**
175
     * Get the value of driver
176
     *
177
     * @return string
178
     */
179
    public function getDriver()
180
    {
181
        return $this->driver;
182
    }
183
184
}
185