PaymentPlanTrait::getPaymentPlanInstallments()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 2
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Omnipay\BillPay\Message\ResponseData;
4
5
use DateTime;
6
use SimpleXMLElement;
7
8
/**
9
 * Access payment plan pof pay later in the response
10
 *
11
 * @author    Andreas Lange <[email protected]>
12
 * @copyright 2016, Connox GmbH
13
 * @license   MIT
14
 */
15
trait PaymentPlanTrait
16
{
17
    /**
18
     * @return SimpleXMLElement
19
     *
20
     * @codeCoverageIgnore
21
     */
22
    abstract public function getData();
23
24
    /**
25
     * Extracts the invoice bank account data if it exists
26
     *
27
     * @return array|null
28
     */
29 2
    public function getPaymentPlan()
30
    {
31 2
        if (!$this->hasPaymentPlan()) {
32 1
            return null;
33
        }
34
35 1
        $plan = $this->getData()->hire_purchase[0]->instl_plan[0];
36
37
        $return = [
38 1
            'num_inst' => (string)$plan['num_inst'],
39 1
            'duration' => (string)$plan->calc[0]->duration,
40 1
            'fee_percent' => (string)$plan->calc[0]->fee_percent,
41 1
            'fee_total' => bcdiv((string)$plan->calc[0]->fee_total, 100, 2),
42 1
            'pre_payment' => bcdiv((string)$plan->calc[0]->pre_payment, 100, 2),
43 1
            'total_amount' => bcdiv((string)$plan->calc[0]->total_amount, 100, 2),
44 1
            'eff_anual' => bcdiv((string)$plan->calc[0]->eff_anual, 100, 2),
45 1
            'nominal' => bcdiv((string)$plan->calc[0]->nominal, 100, 2),
46 1
            'instl' => $this->getPaymentPlanInstallments()
47 1
        ];
48
49 1
        return $return;
50
    }
51
52
    /**
53
     * Checks if the node has an invoice bank account node
54
     *
55
     * @return bool
56
     */
57 2
    public function hasPaymentPlan()
58
    {
59 2
        $data = $this->getData();
60
61 2
        return isset($data->hire_purchase) && isset($data->hire_purchase[0]->instl_plan);
62
    }
63
64
    /**
65
     * Return an array with all installments
66
     *
67
     * @return array
68
     */
69 1
    private function getPaymentPlanInstallments()
70
    {
71 1
        $installments = [];
72
73 1
        foreach ($this->getData()->hire_purchase[0]->instl_plan[0]->instl_list[0]->instl as $installment) {
74 1
            $installments[] = [
75 1
                'date' => DateTime::createFromFormat('Ymd', (string)$installment['date'])->format('Y-m-d'),
76 1
                'type' => (string)$installment['type'],
77 1
                'amount' => bcdiv((string)$installment, 100, 2),
78
            ];
79 1
        }
80
81 1
        return $installments;
82
    }
83
}
84