Completed
Push — master ( 5d18c7...cabcf3 )
by Hannes
10s
created

SerialTrimRewriter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 10
c 0
b 0
f 0
ccs 12
cts 12
cp 1
wmc 4
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A rewrite() 0 14 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace byrokrat\banking\Rewriter;
6
7
use byrokrat\banking\AccountNumber;
8
use byrokrat\banking\UndefinedAccount;
9
10
/**
11
 * Rewriter that trims left side ceros from serial number
12
 */
13
class SerialTrimRewriter implements RewriterInterface
14
{
15
    /**
16
     * @var int
17
     */
18
    private $minimalLength;
19
20 4
    public function __construct(int $minimalLength = 0)
21
    {
22 4
        $this->minimalLength = $minimalLength;
23 4
    }
24
25 141
    public function rewrite(AccountNumber $account): AccountNumber
26
    {
27 141
        if ($this->minimalLength && strlen($account->getSerialNumber()) <= $this->minimalLength) {
28 39
            return $account;
29
        }
30
31 139
        return new UndefinedAccount(
32 139
            $account->getRawNumber(),
33 139
            $account->getClearingNumber(),
34 139
            $account->getClearingCheckDigit(),
35 139
            str_pad(ltrim($account->getSerialNumber(), '0'), $this->minimalLength, '0', STR_PAD_LEFT),
36 139
            $account->getCheckDigit()
37
        );
38
    }
39
}
40