Completed
Push — master ( 3bd4e2...776f46 )
by Jesse
04:06
created

CompositeProxyFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 27
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addFactory() 0 3 1
A decidingBy() 0 3 1
A __construct() 0 5 2
A create() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Proxy;
4
5
/**
6
 * CompositeProxyFactory.
7
 *
8
 * @author Stratadox
9
 */
10
final class CompositeProxyFactory implements ProxyFactory
11
{
12
    private $key;
13
    /** @var ProxyFactory[] */
14
    private $factories;
15
16
    public function __construct(string $decisionKey, array $factories)
17
    {
18
        $this->key = $decisionKey;
19
        foreach ($factories as $key => $factory) {
20
            $this->addFactory($key, $factory);
21
        }
22
    }
23
24
    private function addFactory(string $key, ProxyFactory $factory): void
25
    {
26
        $this->factories[$key] = $factory;
27
    }
28
29
    public static function decidingBy(string $key, array $factories): self
30
    {
31
        return new self($key, $factories);
32
    }
33
34
    public function create(array $knownData = []): Proxy
35
    {
36
        return $this->factories[$knownData[$this->key]]->create($knownData);
37
    }
38
}
39