Completed
Push — master ( dbd889...95c92d )
by Alberto
02:45
created

ProxyBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 58
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getProxy() 0 10 3
A reset() 0 4 1
A buildProxy() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Builder;
5
6
use Moka\Exception\InvalidIdentifierException;
7
use Moka\Exception\MockNotCreatedException;
8
use Moka\Factory\ProxyFactory;
9
use Moka\Proxy\Proxy;
10
use Moka\Proxy\ProxyContainer;
11
use Moka\Strategy\MockingStrategyInterface;
12
13
/**
14
 * Class ProxyBuilder
15
 * @package Moka\Builder
16
 */
17
class ProxyBuilder
18
{
19
    /**
20
     * @var ProxyContainer
21
     */
22
    protected $container;
23
24
    /**
25
     * @var MockingStrategyInterface
26
     */
27
    protected $mockingStrategy;
28
29
    /**
30
     * ProxyBuilder constructor.
31
     * @param MockingStrategyInterface $mockingStrategy
32
     */
33 4
    public function __construct(MockingStrategyInterface $mockingStrategy)
34
    {
35 4
        $this->mockingStrategy = $mockingStrategy;
36 4
        $this->reset();
37
    }
38
39
    /**
40
     * @return void
41
     */
42 6
    public function reset()
43
    {
44 6
        $this->container = new ProxyContainer();
45
    }
46
47
    /**
48
     * @param string $fqcn
49
     * @param string|null $alias
50
     * @return Proxy
51
     *
52
     * @throws MockNotCreatedException
53
     * @throws InvalidIdentifierException
54
     */
55 7
    public function getProxy(string $fqcn, string $alias = null): Proxy
56
    {
57 7
        $alias = $alias ?: $fqcn;
58
59 7
        if (!$this->container->has($alias)) {
60 5
            $this->container->set($alias, $this->buildProxy($fqcn));
61
        }
62
63 7
        return $this->container->get($alias);
64
    }
65
66
    /**
67
     * @param string $fqcn
68
     * @return Proxy
69
     */
70 5
    protected function buildProxy(string $fqcn): Proxy
71
    {
72 5
        return ProxyFactory::get($fqcn, $this->mockingStrategy);
73
    }
74
}
75