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

ProxyBuilder::getProxy()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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