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

PostalAccount   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 5
c 2
b 0
f 2
lcom 1
cbo 0
dl 0
loc 71
ccs 21
cts 21
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A format() 0 4 1
A checkPrefix() 0 11 1
1
<?php
2
3
namespace Z38\SwissPayment;
4
5
/**
6
 * PostalAccount holds details about a PostFinance account
7
 */
8
class PostalAccount
9
{
10
    const PATTERN = '/^[0-9]{2}-[1-9][0-9]{0,5}-[0-9]$/';
11
12
    /**
13
     * @var int
14
     */
15
    protected $prefix;
16
17
    /**
18
     * @var int
19
     */
20
    protected $number;
21
22
    /**
23
     * @var int
24
     */
25
    protected $checkDigit;
26
27
    /**
28
     * Constructor
29
     *
30
     * @param string $postalAccount
31
     *
32
     * @throws \InvalidArgumentException When the account number is not valid (check digit is not being tested).
33
     */
34 8
    public function __construct($postalAccount)
35
    {
36 8
        if (!preg_match(self::PATTERN, $postalAccount)) {
37 1
            throw new \InvalidArgumentException('Postal account number is not properly formatted.');
38
        }
39
40 7
        $parts = explode('-', $postalAccount);
41 7
        if (!self::checkPrefix($parts[0])) {
42 1
            throw new \InvalidArgumentException('Postal account number has an invalid prefix.');
43
        }
44
45 6
        $this->prefix = (int) $parts[0];
46 6
        $this->number = (int) $parts[1];
47 6
        $this->checkDigit = (int) $parts[2];
48 6
    }
49
50
    /**
51
     * Format the postal account number
52
     *
53
     * @return string The formatted account number
54
     */
55 6
    public function format()
56
    {
57 6
        return sprintf('%d-%d-%d', $this->prefix, $this->number, $this->checkDigit);
58
    }
59
60
    /**
61
     * Checks whether a given prefix is valid
62
     *
63
     * @param int $prefix The prefix to be checked
64
     *
65
     * @return bool True if the prefix is valid
66
     */
67 2
    private static function checkPrefix($prefix)
68
    {
69 2
        return in_array($prefix, array(
70 2
            10, 12, 17, 18, 19,
71 2
            20, 23, 25, 30, 34,
72 2
            40, 45, 46, 49, 50,
73 2
            60, 65, 69, 70, 80,
74 2
            82, 84, 85, 87, 90,
75 2
            91, 92,
76 2
        ));
77
    }
78
}
79