Completed
Pull Request — master (#6)
by Alberto
04:09
created

Proxy::buildMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
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\Factory\StubFactory;
10
use Moka\Strategy\MockingStrategyInterface;
11
use Moka\Stub\StubSet;
12
13
/**
14
 * Class Proxy
15
 * @package Moka\Proxy
16
 */
17
class Proxy
18
{
19
    /**
20
     * @var string
21
     */
22
    private $fqcn;
23
24
    /**
25
     * @var StubSet
26
     */
27
    private $stubs;
28
29
    /**
30
     * @var MockingStrategyInterface
31
     */
32
    private $mockingStrategy;
33
34
    /**
35
     * @var object
36
     */
37
    private $mock;
38
39
    /**
40
     * Proxy constructor.
41
     * @param string $fqcn
42
     * @param MockingStrategyInterface $mockingStrategy
43
     */
44 19
    public function __construct(string $fqcn, MockingStrategyInterface $mockingStrategy)
45
    {
46 19
        $this->fqcn = $fqcn;
47 19
        $this->mockingStrategy = $mockingStrategy;
48 19
        $this->resetStubs();
49
    }
50
51
    /**
52
     * @param array $methodsWithValues
53
     * @return Proxy
54
     *
55
     * @throws InvalidArgumentException
56
     */
57 4
    public function stub(array $methodsWithValues): self
58
    {
59 4
        $this->stubs->addAll(
60 4
            StubFactory::fromArray($methodsWithValues)->all()
61
        );
62
63 4
        if ($this->mock) {
64 4
            $this->decorateMock();
65
        }
66
67 4
        return $this;
68
    }
69
70
    /**
71
     * @return object
72
     *
73
     * @throws MockNotServedException
74
     */
75 12
    public function serve()
76
    {
77
        try {
78 12
            if (!$this->mock) {
79 12
                $this->buildMock();
80
            }
81
82 12
            return $this->mockingStrategy->get($this->mock);
83 2
        } catch (MockNotCreatedException $exception) {
84 2
            throw new MockNotServedException($exception->getMessage());
85
        }
86
    }
87
88
    /**
89
     * @return void
90
     */
91 19
    private function resetStubs()
92
    {
93 19
        $this->stubs = new StubSet();
94
    }
95
96
    /**
97
     * @return object
98
     *
99
     * @throws MockNotCreatedException
100
     */
101 12
    private function buildMock()
102
    {
103 12
        $this->mock = $this->mockingStrategy->build($this->fqcn);
104 12
        $this->decorateMock();
105
106 12
        return $this->mock;
107
    }
108
109
    /**
110
     * @return void
111
     *
112
     * @throws InvalidArgumentException
113
     */
114 12
    private function decorateMock()
115
    {
116 12
        $this->mockingStrategy->decorate($this->mock, $this->stubs);
117 12
        $this->resetStubs();
118
    }
119
}
120