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

Moka::__callStatic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka;
5
6
use Moka\Exception\InvalidIdentifierException;
7
use Moka\Exception\MockNotCreatedException;
8
use Moka\Exception\NotImplementedException;
9
use Moka\Factory\ProxyBuilderFactory;
10
use Moka\Proxy\Proxy;
11
use Moka\Strategy\MockeryMockingStrategy;
12
use Moka\Strategy\MockingStrategyInterface;
13
use Moka\Strategy\PHPUnitMockingStrategy;
14
use Moka\Strategy\ProphecyMockingStrategy;
15
16
/**
17
 * Class Moka
18
 * @package Moka
19
 *
20
 * @method static Proxy mockery(string $fqcn, string $alias = null)
21
 * @method static Proxy phpunit(string $fqcn, string $alias = null)
22
 * @method static Proxy prophecy(string $fqcn, string $alias = null)
23
 */
24
class Moka
25
{
26
    const STRATEGIES = [
27
        'mockery' => MockeryMockingStrategy::class,
28
        'phpunit' => PHPUnitMockingStrategy::class,
29
        'prophecy' => ProphecyMockingStrategy::class
30
    ];
31
32
    /**
33
     * @param string $fqcn
34
     * @param string|null $alias
35
     * @return Proxy
36
     *
37
     * @throws MockNotCreatedException
38
     * @throws InvalidIdentifierException
39
     */
40 5
    public static function brew(string $fqcn, string $alias = null, MockingStrategyInterface $mockingStrategy = null): Proxy
41
    {
42 5
        return ProxyBuilderFactory::get($mockingStrategy ?: new PHPUnitMockingStrategy())
43 5
            ->getProxy($fqcn, $alias);
44
    }
45
46
    /**
47
     * @param string $name
48
     * @param array $arguments
49
     * @return Proxy
50
     *
51
     * @throws NotImplementedException
52
     */
53 2
    public static function __callStatic($name, $arguments)
54
    {
55 2
        if (!isset(static::STRATEGIES[$name])) {
56 1
            throw new NotImplementedException(
57 1
                sprintf(
58 1
                    'Mocking strategy "%s" does not exist',
59 1
                    $name
60
                )
61
            );
62
        }
63
64 1
        $fqcn = $arguments[0];
65 1
        $alias = $arguments[1] ?? null;
66 1
        $mockingStrategy = static::STRATEGIES[$name];
67
68 1
        return self::brew($fqcn, $alias, new $mockingStrategy());
69
    }
70
71
    /**
72
     * @return void
73
     */
74 2
    public static function clean()
75
    {
76 2
        ProxyBuilderFactory::reset();
77
    }
78
79
    /**
80
     * @param string $fqcn
81
     * @param string|null $alias
82
     * @return Proxy
83
     *
84
     * @throws MockNotCreatedException
85
     * @throws InvalidIdentifierException
86
     *
87
     * @deprecated since 0.3.0
88
     */
89 1
    public static function get(string $fqcn, string $alias = null): Proxy
90
    {
91 1
        return static::brew($fqcn, $alias, new PHPUnitMockingStrategy());
92
    }
93
}
94