ProxyGenerator::getInstance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 1
rs 9.9666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Generator;
5
6
use Moka\Exception\InvalidArgumentException;
7
use Moka\Exception\MockNotCreatedException;
8
use Moka\Generator\Template\ClassTemplate;
9
use Moka\Proxy\ProxyInterface;
10
use Moka\Proxy\ProxyTrait;
11
use Moka\Strategy\MockingStrategyInterface;
12
13
/**
14
 * Class ProxyGenerator
15
 * @package Moka\Generator
16
 */
17
class ProxyGenerator
18
{
19
    /**
20
     * @var MockingStrategyInterface
21
     */
22
    private $mockingStrategy;
23
24
    /**
25
     * ProxyGenerator constructor.
26
     * @param MockingStrategyInterface $mockingStrategy
27
     */
28 4
    public function __construct(MockingStrategyInterface $mockingStrategy)
29
    {
30 4
        $this->mockingStrategy = $mockingStrategy;
31
    }
32
33
    /**
34
     * @param string $fqcn
35
     * @return ProxyInterface
36
     *
37
     * @throws MockNotCreatedException
38
     * @throws InvalidArgumentException
39
     * @throws \ReflectionException
40
     */
41 10
    public function get(string $fqcn): ProxyInterface
42
    {
43 10
        $mock = $this->mockingStrategy->build($fqcn);
44 10
        $mockFQCN = \get_class($this->mockingStrategy->get($mock));
45 10
        $mockClass = new \ReflectionClass($mockFQCN);
46
47 10
        $proxyCode = ClassTemplate::generate($mockClass);
48 10
        $proxyFQCN = eval($proxyCode);
49
50 10
        return $this->getInstance($proxyFQCN)
51 10
            ->__moka_setMock($mock)
52 10
            ->__moka_setMockingStrategy($this->mockingStrategy);
53
    }
54
55
    /**
56
     * @param string $proxyFQCN
57
     * @return ProxyInterface|ProxyTrait
58
     * @throws \ReflectionException
59
     */
60 10
    protected function getInstance(string $proxyFQCN): ProxyInterface
61
    {
62 10
        $proxyClass = new \ReflectionClass($proxyFQCN);
63
64
        /** @var ProxyInterface|ProxyTrait $proxy */
65 10
        $proxy = $proxyClass->newInstance();
66
67 10
        return $proxy;
68
    }
69
}
70