Completed
Pull Request — master (#17)
by Angelo
05:07
created

Proxy   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 110
ccs 23
cts 25
cp 0.92
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A resetStubs() 0 4 1
A __call() 0 4 1
A getMock() 0 9 2
A decorateMock() 0 5 1
A stub() 0 10 1
A serve() 0 8 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Proxy;
5
6
use Moka\Exception\InvalidArgumentException;
7
use Moka\Exception\MockNotCreatedException;
8
use Moka\Exception\MockNotServedException;
9
use Moka\Exception\NotImplementedException;
10
use Moka\Factory\StubFactory;
11
use Moka\Strategy\MockingStrategyInterface;
12
use Moka\Stub\StubSet;
13
14
/**
15
 * Class Proxy
16
 * @package Moka\Proxy
17
 */
18
class Proxy
19
{
20
    /**
21
     * @var string
22
     */
23
    private $fqcn;
24
25
    /**
26
     * @var StubSet
27
     */
28
    private $stubs;
29
30
    /**
31
     * @var MockingStrategyInterface
32
     */
33
    private $mockingStrategy;
34
35
    /**
36
     * @var object
37
     */
38
    private $mock;
39
40
    /**
41
     * Proxy constructor.
42
     * @param string $fqcn
43
     * @param MockingStrategyInterface $mockingStrategy
44
     */
45 9
    public function __construct(string $fqcn, MockingStrategyInterface $mockingStrategy)
46
    {
47 9
        $this->fqcn = $fqcn;
48 9
        $this->mockingStrategy = $mockingStrategy;
49 9
        $this->resetStubs();
50
    }
51
52
    /**
53
     * @return void
54
     */
55 9
    private function resetStubs()
56
    {
57 9
        $this->stubs = new StubSet();
58
    }
59
60
    public function __call($name, $arguments)
61
    {
62
        $this->getMock()->{$name}(...$arguments);
63
    }
64
65
    /**
66
     * @return object
67
     *
68
     * @throws MockNotCreatedException
69
     * @throws NotImplementedException
70
     * @throws InvalidArgumentException
71
     */
72 4
    private function getMock()
73
    {
74 4
        if (!$this->mock) {
75 4
            $this->mock = $this->mockingStrategy->build($this->fqcn);
76 4
            $this->decorateMock();
77
        }
78
79 4
        return $this->mock;
80
    }
81
82
    /**
83
     * @return void
84
     *
85
     * @throws InvalidArgumentException
86
     * @throws MockNotCreatedException
87
     * @throws NotImplementedException
88
     */
89 4
    private function decorateMock()
90
    {
91 4
        $this->mockingStrategy->decorate($this->getMock(), $this->stubs);
92 4
        $this->resetStubs();
93
    }
94
95
    /**
96
     * @param array $methodsWithValues
97
     * @return Proxy
98
     *
99
     * @throws InvalidArgumentException
100
     * @throws MockNotCreatedException
101
     * @throws NotImplementedException
102
     */
103 2
    public function stub(array $methodsWithValues): self
104
    {
105 2
        $this->stubs->addAll(
106 2
            StubFactory::fromArray($methodsWithValues)->all()
107
        );
108
109 2
        $this->decorateMock();
110
111 2
        return $this;
112
    }
113
114
    /**
115
     * @return object
116
     *
117
     * @throws MockNotServedException
118
     */
119 3
    public function serve()
120
    {
121
        try {
122 3
            return $this->mockingStrategy->get($this->getMock());
123 1
        } catch (\Exception $exception) {
124 1
            throw new MockNotServedException($exception->getMessage());
125
        }
126
    }
127
}
128