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

ProxyBuilder::clean()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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