Completed
Push — scrutinizer ( 42ac6a...7bc18e )
by z38
02:40
created

BC::fromIBAN()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Z38\SwissPayment;
4
5
/**
6
 * BC holds a Swiss bank clearing number
7
 */
8
class BC implements FinancialInstitutionInterface
9
{
10
    const PATTERN = '/^[0-9]{3,5}$/';
11
12
    /**
13
     * @var int
14
     */
15
    protected $bc;
16
17
    /**
18
     * Constructor
19
     *
20
     * @param string $bic
0 ignored issues
show
Bug introduced by
There is no parameter named $bic. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
21
     *
22
     * @throws \InvalidArgumentException When the BC does contain invalid characters or the length does not match.
23
     */
24 5
    public function __construct($bc)
25
    {
26 5
        $bc = (string) $bc;
27 5
        if (!preg_match(self::PATTERN, $bc)) {
28 2
            throw new \InvalidArgumentException('BC is not properly formatted.');
29
        }
30
31 3
        $this->bc = (int) ltrim($bc, '0');
32 3
    }
33
34
    /**
35
     * Extracts the BC from an IBAN
36
     *
37
     * @param IBAN $iban
38
     *
39
     * @throws \InvalidArgumentException When the supplied IBAN is not from Switzerland
40
     */
41 4
    public static function fromIBAN(IBAN $iban)
42
    {
43 4
        if ($iban->getCountry() !== 'CH') {
44 1
            throw new \InvalidArgumentException('BC can only be extracted from Swiss IBANs.');
45
        }
46
47 3
        return new self(substr($iban->normalize(), 4, 5));
48
    }
49
50
    /**
51
     * Returns a formatted representation of the BIC
52
     *
53
     * @return string The formatted BIC
54
     */
55 3
    public function format()
56
    {
57 3
        return sprintf('%d', $this->bc);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 2
    public function asDom(\DOMDocument $doc)
64
    {
65 2
        $xml = $doc->createElement('FinInstnId');
66 2
        $clearingSystem = $doc->createElement('ClrSysMmbId');
67 2
        $clearingSystemId = $doc->createElement('ClrSysId');
68 2
        $clearingSystemId->appendChild($doc->createElement('Cd', 'CHBCC'));
69 2
        $clearingSystem->appendChild($clearingSystemId);
70 2
        $clearingSystem->appendChild($doc->createElement('MmbId', $this->format()));
71 2
        $xml->appendChild($clearingSystem);
72
73 2
        return $xml;
74
    }
75
}
76