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

DelegatingFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
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