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

SerialTrimRewriter::rewrite()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
ccs 9
cts 9
cp 1
crap 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