InvoiceBankAccountTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 59
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
getData() 0 1 ?
A getInvoiceBankAccount() 0 18 2
A hasInvoiceBankAccount() 0 6 2
A formatDate() 0 8 2
1
<?php
2
3
namespace Omnipay\BillPay\Message\ResponseData;
4
5
use DateTime;
6
use SimpleXMLElement;
7
8
/**
9
 * Access invoice bank account in the response
10
 *
11
 * @author    Andreas Lange <[email protected]>
12
 * @copyright 2016, Connox GmbH
13
 * @license   MIT
14
 */
15
trait InvoiceBankAccountTrait
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 5
    public function getInvoiceBankAccount()
30
    {
31 5
        if (!$this->hasInvoiceBankAccount()) {
32 2
            return null;
33
        }
34
35 3
        $data = $this->getData();
36
37
        return [
38 3
            'account_holder' => (string)$data->invoice_bank_account['account_holder'],
39 3
            'account_number' => (string)$data->invoice_bank_account['account_number'],
40 3
            'bank_code' => (string)$data->invoice_bank_account['bank_code'],
41 3
            'bank_name' => (string)$data->invoice_bank_account['bank_name'],
42 3
            'invoice_reference' => (string)$data->invoice_bank_account['invoice_reference'],
43 3
            'invoice_duedate' => $this->formatDate((string)$data->invoice_bank_account['invoice_duedate']),
44 3
            'activation_performed' => (string)$data->invoice_bank_account['activation_performed'] === '1',
45 3
        ];
46
    }
47
48
    /**
49
     * Checks if the node has an invoice bank account node
50
     *
51
     * @return bool
52
     */
53 5
    public function hasInvoiceBankAccount()
54
    {
55 5
        $data = $this->getData();
56
57 5
        return isset($data->invoice_bank_account) && (string)$data->invoice_bank_account['account_holder'] !== '';
58
    }
59
60
    /**
61
     * @param string $date Date with format 'Ymd'
62
     *
63
     * @return null|string Y-m-d or null
64
     */
65 3
    private function formatDate($date)
66
    {
67 3
        if (!$date) {
68 2
            return null;
69
        }
70
71 1
        return DateTime::createFromFormat('Ymd', $date)->format('Y-m-d');
72
    }
73
}
74