Completed
Pull Request — master (#24)
by Angelo
09:01 queued 07:02
created

ProxyBuilderFactory::get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
crap 3
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 3
    public static function get(MockingStrategyInterface $mockingStrategy): ProxyBuilder
25
    {
26 3
        $key = self::key($mockingStrategy);
27 3
        if (!array_key_exists($key, self::$mockBuilders) || !self::$mockBuilders[$key] instanceof ProxyBuilder) {
28 2
            self::$mockBuilders[$key] = static::build($mockingStrategy);
29
        }
30
31 3
        return self::$mockBuilders[$key];
32
    }
33
34
    /**
35
     * @return void
36
     */
37 1
    public static function reset()
38
    {
39 1
        foreach (self::$mockBuilders as $mockBuilder) {
40 1
            $mockBuilder->reset();
41
        }
42
    }
43
44
    /**
45
     * @param MockingStrategyInterface $mockingStrategy
46
     * @return ProxyBuilder
47
     */
48 2
    protected static function build(MockingStrategyInterface $mockingStrategy): ProxyBuilder
49
    {
50 2
        return new ProxyBuilder($mockingStrategy);
51
    }
52
53
    /**
54
     * @param MockingStrategyInterface $mockingStrategy
55
     * @return string
56
     */
57 3
    private static function key(MockingStrategyInterface $mockingStrategy): string
58
    {
59 3
        return get_class($mockingStrategy);
60
    }
61
}
62