Completed
Push — master ( 360209...6b1bfe )
by Hannes
02:57 queued 01:42
created

DelegatingFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createAccount() 0 16 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace byrokrat\banking;
6
7
use byrokrat\banking\Exception\InvalidAccountNumberException;
8
9
/**
10
 * Factory that delegates to other factories
11
 */
12
class DelegatingFactory implements AccountFactoryInterface
13
{
14
    /**
15
     * @var AccountFactoryInterface[]
16
     */
17
    private $factories;
18
19 3
    public function __construct(AccountFactoryInterface ...$factories)
20
    {
21 3
        $this->factories = $factories;
22 3
    }
23
24 3
    public function createAccount(string $number): AccountNumber
25
    {
26 3
        $errors = [];
27
28 3
        foreach ($this->factories as $factory) {
29
            try {
30 3
                return $factory->createAccount($number);
31 2
            } catch (Exception $e) {
32 2
                $errors[] = $e->getMessage();
33
            }
34
        }
35
36 1
        throw new InvalidAccountNumberException(
37 1
            "Unable to create account for the following resons:\n" . implode("\n", $errors)
38
        );
39
    }
40
}
41