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

ProxyBuilderFactory::reset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Factory;
5
6
use Moka\Builder\ProxyBuilder;
7
use Moka\Strategy\MockingStrategyInterface;
8
9
/**
10
 * Class ProxyBuilderFactory
11
 * @package Moka\Factory
12
 */
13
class ProxyBuilderFactory
14
{
15
    /**
16
     * @var array|ProxyBuilder[]
17
     */
18
    private static $mockBuilders = [];
19
20
    /**
21
     * @param MockingStrategyInterface $mockingStrategy
22
     * @return ProxyBuilder
23
     */
24 6
    public static function get(MockingStrategyInterface $mockingStrategy): ProxyBuilder
25
    {
26 6
        $key = self::key($mockingStrategy);
27 6
        if (!array_key_exists($key, self::$mockBuilders) || !self::$mockBuilders[$key] instanceof ProxyBuilder) {
28 2
            self::$mockBuilders[$key] = static::build(new $mockingStrategy);
29
        }
30
31 6
        return self::$mockBuilders[$key];
32
    }
33
34
    /**
35
     * @param MockingStrategyInterface $mockingStrategy
36
     * @return string
37
     */
38 6
    private static function key(MockingStrategyInterface $mockingStrategy): string
39
    {
40 6
        return get_class($mockingStrategy);
41
    }
42
43
    /**
44
     * @param MockingStrategyInterface $mockingStrategy
45
     * @return ProxyBuilder
46
     */
47 2
    protected static function build(MockingStrategyInterface $mockingStrategy): ProxyBuilder
48
    {
49 2
        return new ProxyBuilder($mockingStrategy);
50
    }
51
52 2
    public static function reset()
53
    {
54 2
        foreach (self::$mockBuilders as $mockBuilder) {
55 2
            $mockBuilder->reset();
56
        }
57
    }
58
}
59