BankgiroFactory::createAccount()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 18
loc 18
rs 9.6666
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;
6
7
use byrokrat\banking\Exception\InvalidAccountNumberException;
8
use byrokrat\banking\Validator\CheckDigitType2Validator;
9
use byrokrat\banking\Validator\ValidatorInterface;
10
11
/**
12
 * Bankgiro account number factory
13
 */
14 View Code Duplication
class BankgiroFactory implements AccountFactoryInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
{
16
    /**
17
     * @var ValidatorInterface
18
     */
19
    private $validator;
20
21 17
    public function __construct()
22
    {
23 17
        $this->validator = new CheckDigitType2Validator;
24 17
    }
25
26 17
    public function createAccount(string $number): AccountNumber
27
    {
28 17
        if (!preg_match('/^0*(\d{3,4})-?(\d{3})(\d)$/', $number, $matches)) {
29 12
            throw new InvalidAccountNumberException("Invalid bankgiro account number structure");
30
        }
31
32 5
        $account = new Bankgiro($number, $matches[1].$matches[2], $matches[3]);
33
34 5
        $result = $this->validator->validate($account);
35
36 5
        if (!$result->isValid()) {
37 1
            throw new InvalidAccountNumberException(
38 1
                "Unable to parse bankgiro $number: {$result->getMessage()}"
39
            );
40
        }
41
42 4
        return $account;
43
    }
44
}
45