Statement::arrayFromResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Mukhin\PrivatbankBundle\Model;
4
5
use Mukhin\PrivatbankBundle\Exception\PrivatbankResponseParsingException;
6
7
class Statement
8
{
9
    /** @var string */
10
    protected $cardNumber;
11
12
    /** @var \DateTime */
13
    protected $transactionDate;
14
15
    /** @var string */
16
    protected $transactionCode;
17
18
    /** @var float */
19
    protected $sourceAmount;
20
21
    /** @var float */
22
    protected $sourceFee;
23
24
    /** @var  string */
25
    protected $sourceCurrency;
26
27
    /** @var float */
28
    protected $exchangeRate;
29
30
    /** @var float */
31
    protected $amount;
32
33
    /** @var string */
34
    protected $currency;
35
36
    /** @var float */
37
    protected $balance;
38
39
    /** @var string */
40
    protected $description;
41
42
    /**
43
     * @param \SimpleXMLElement $statements
44
     * @return $this[]
45
     */
46
    public static function arrayFromResponse(\SimpleXMLElement $statements)
47
    {
48
        $result = [];
49
        foreach ($statements->statement as $statement) {
50
            $result[] = self::fromResponse($statement);
51
        }
52
        return $result;
53
    }
54
55
    /**
56
     * @param \SimpleXMLElement $statement
57
     * @return $this
58
     * @throws PrivatbankResponseParsingException
59
     */
60
    public static function fromResponse(\SimpleXMLElement $statement)
61
    {
62
        list($sourceAmount, $sourceCurrency) = explode(' ', (string)$statement['amount']);
63
        list($amount, $currency) = explode(' ', (string)$statement['cardamount']);
64
        list($balance,) = explode(' ', (string)$statement['rest']);
65
        $transactionDate = \DateTime::createFromFormat(
66
            'Y-m-d H:i:s',
67
            sprintf('%s %s', (string)$statement['trandate'], (string)$statement['trantime']),
68
            new \DateTimeZone('Europe/Kiev')
69
        );
70
71
        if (false === $transactionDate) {
72
            throw new PrivatbankResponseParsingException("Error occured during parsing transaction date from Privatbank API endpoint response");
73
        }
74
75
        return (new self)
76
            ->setCardNumber((string)$statement['card'])
77
            ->setTransactionDate($transactionDate)
78
            ->setTransactionCode((string)$statement['appcode'])
79
            ->setAmount(floatval($amount))
80
            ->setBalance(floatval($balance))
81
            ->setCurrency($currency)
82
            ->setSourceAmount(floatval($sourceAmount))
83
            ->setSourceCurrency($sourceCurrency)
84
            ->setDescription((string)$statement['terminal'])
85
            ->setExchangeRate($currency != $sourceCurrency ? abs(floatval($amount)/floatval($sourceAmount)) : null)
86
            ->setSourceFee($currency == $sourceCurrency ? abs(abs(floatval($amount)) - abs(floatval($sourceAmount))) : null)
87
        ;
88
    }
89
90
    /**
91
     * @param string $cardNumber
92
     *
93
     * @return $this
94
     */
95
    public function setCardNumber($cardNumber)
96
    {
97
        $this->cardNumber = $cardNumber;
98
99
        return $this;
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function getCardNumber()
106
    {
107
        return $this->cardNumber;
108
    }
109
110
    /**
111
     * @param \DateTime $transactionDate
112
     *
113
     * @return $this
114
     */
115
    public function setTransactionDate(\DateTime $transactionDate)
116
    {
117
        $this->transactionDate = $transactionDate;
118
119
        return $this;
120
    }
121
122
    /**
123
     * @return \DateTime
124
     */
125
    public function getTransactionDate()
126
    {
127
        return $this->transactionDate;
128
    }
129
130
    /**
131
     * @param string $transactionCode
132
     *
133
     * @return $this
134
     */
135
    public function setTransactionCode($transactionCode)
136
    {
137
        $this->transactionCode = $transactionCode;
138
139
        return $this;
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function getTransactionCode()
146
    {
147
        return $this->transactionCode;
148
    }
149
150
    /**
151
     * @param float $sourceAmount
152
     *
153
     * @return $this
154
     */
155
    public function setSourceAmount($sourceAmount)
156
    {
157
        $this->sourceAmount = $sourceAmount;
158
159
        return $this;
160
    }
161
162
    /**
163
     * @return float
164
     */
165
    public function getSourceAmount()
166
    {
167
        return $this->sourceAmount;
168
    }
169
170
    /**
171
     * @param float $sourceFee
172
     *
173
     * @return $this
174
     */
175
    public function setSourceFee($sourceFee)
176
    {
177
        $this->sourceFee = $sourceFee;
178
179
        return $this;
180
    }
181
182
    /**
183
     * @return float
184
     */
185
    public function getSourceFee()
186
    {
187
        return $this->sourceFee;
188
    }
189
190
    /**
191
     * @param string $sourceCurrency
192
     *
193
     * @return $this
194
     */
195
    public function setSourceCurrency($sourceCurrency)
196
    {
197
        $this->sourceCurrency = $sourceCurrency;
198
199
        return $this;
200
    }
201
202
    /**
203
     * @return string
204
     */
205
    public function getSourceCurrency()
206
    {
207
        return $this->sourceCurrency;
208
    }
209
210
    /**
211
     * @param float $exchangeRate
212
     *
213
     * @return $this
214
     */
215
    public function setExchangeRate($exchangeRate)
216
    {
217
        $this->exchangeRate = $exchangeRate;
218
219
        return $this;
220
    }
221
222
    /**
223
     * @return float
224
     */
225
    public function getExchangeRate()
226
    {
227
        return $this->exchangeRate;
228
    }
229
230
    /**
231
     * @param float $amount
232
     *
233
     * @return $this
234
     */
235
    public function setAmount($amount)
236
    {
237
        $this->amount = $amount;
238
239
        return $this;
240
    }
241
242
    /**
243
     * @return float
244
     */
245
    public function getAmount()
246
    {
247
        return $this->amount;
248
    }
249
250
    /**
251
     * @return string
252
     */
253
    public function getSignedAmount()
254
    {
255
        return $this->amount > 0 ? sprintf('+%s', $this->amount) : $this->amount;
256
    }
257
258
    /**
259
     * @param float $balance
260
     *
261
     * @return $this
262
     */
263
    public function setBalance($balance)
264
    {
265
        $this->balance = $balance;
266
267
        return $this;
268
    }
269
270
    /**
271
     * @return float
272
     */
273
    public function getBalance()
274
    {
275
        return $this->balance;
276
    }
277
278
    /**
279
     * @param string $currency
280
     *
281
     * @return $this
282
     */
283
    public function setCurrency($currency)
284
    {
285
        $this->currency = $currency;
286
287
        return $this;
288
    }
289
290
    /**
291
     * @return string
292
     */
293
    public function getCurrency()
294
    {
295
        return $this->currency;
296
    }
297
298
    /**
299
     * @param string $description
300
     *
301
     * @return $this
302
     */
303
    public function setDescription($description)
304
    {
305
        $this->description = $description;
306
307
        return $this;
308
    }
309
310
    /**
311
     * @return string
312
     */
313
    public function getDescription()
314
    {
315
        return $this->description;
316
    }
317
}