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

Proxy::decorateMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\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 10
    public function __construct(string $fqcn, MockingStrategyInterface $mockingStrategy)
46
    {
47 10
        $this->fqcn = $fqcn;
48 10
        $this->mockingStrategy = $mockingStrategy;
49 10
        $this->resetStubs();
50
    }
51
52
    /**
53
     * @return void
54
     */
55 10
    private function resetStubs()
56
    {
57 10
        $this->stubs = new StubSet();
58
    }
59
60
    /**
61
     * @param array $methodsWithValues
62
     * @return Proxy
63
     *
64
     * @throws InvalidArgumentException
65
     */
66 2
    public function stub(array $methodsWithValues): self
67
    {
68 2
        $this->stubs->addAll(
69 2
            StubFactory::fromArray($methodsWithValues)->all()
70
        );
71
72 2
        if ($this->mock) {
73 1
            $this->decorateMock();
74
        }
75
76 2
        return $this;
77
    }
78
79
    /**
80
     * @return void
81
     *
82
     * @throws InvalidArgumentException
83
     */
84 3
    private function decorateMock()
85
    {
86 3
        $this->mockingStrategy->decorate($this->mock, $this->stubs);
87 3
        $this->resetStubs();
88
    }
89
90
    /**
91
     * @return object
92
     *
93
     * @throws MockNotServedException
94
     */
95 3
    public function serve()
96
    {
97
        try {
98 3
            if (!$this->mock) {
99 3
                $this->buildMock();
100
            }
101
102 3
            return $this->mockingStrategy->get($this->mock);
103 1
        } catch (\Exception $exception) {
104 1
            throw new MockNotServedException($exception->getMessage());
105
        }
106
    }
107
108
    /**
109
     * @return object
110
     *
111
     * @throws MockNotCreatedException
112
     * @throws NotImplementedException
113
     * @throws InvalidArgumentException
114
     */
115 3
    private function buildMock()
116
    {
117 3
        $this->mock = $this->mockingStrategy->build($this->fqcn);
118 3
        $this->decorateMock();
119
120 3
        return $this->mock;
121
    }
122
}
123