Completed
Pull Request — master (#31)
by Angelo
02:07
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\Strategy\MockingStrategyInterface;
10
11
/**
12
 * Class Proxy
13
 * @package Moka\Proxy
14
 */
15
class Proxy
16
{
17
    /**
18
     * @var string
19
     */
20
    private $fqcn;
21
22
    /**
23
     * @var MockingStrategyInterface
24
     */
25
    private $mockingStrategy;
26
27
    /**
28
     * @var object
29
     */
30
    private $mock;
31
32
    /**
33
     * Proxy constructor.
34
     * @param string $fqcn
35
     * @param MockingStrategyInterface $mockingStrategy
36
     */
37 13
    public function __construct(string $fqcn, MockingStrategyInterface $mockingStrategy)
38
    {
39 13
        $this->fqcn = $fqcn;
40 13
        $this->mockingStrategy = $mockingStrategy;
41
    }
42
43
    /**
44
     * @param string $name
45
     * @param array $arguments
46
     * @return mixed
47
     *
48
     * @throws MockNotCreatedException
49
     */
50 2
    public function __call(string $name, array $arguments)
51
    {
52 2
        return $this->getMock()->$name(...$arguments);
53
    }
54
55
    /**
56
     * @return object
57
     *
58
     * @throws MockNotCreatedException
59
     */
60 6
    private function getMock()
61
    {
62 6
        if (!$this->mock) {
63 6
            $this->mock = $this->mockingStrategy->build($this->fqcn);
64
        }
65
66 6
        return $this->mock;
67
    }
68
69
    /**
70
     * @param array $methodsWithValues
71
     * @return Proxy
72
     *
73
     * @throws InvalidArgumentException
74
     * @throws MockNotCreatedException
75
     */
76 2
    public function stub(array $methodsWithValues): self
77
    {
78 2
        $this->mockingStrategy->decorate($this->getMock(), $methodsWithValues);
79
80 2
        return $this;
81
    }
82
83
    /**
84
     * @return object
85
     *
86
     * @throws MockNotServedException
87
     */
88 3
    public function serve()
89
    {
90
        try {
91 3
            return $this->mockingStrategy->get($this->getMock());
92 1
        } catch (\Exception $exception) {
93 1
            throw new MockNotServedException($exception->getMessage());
94
        }
95
    }
96
}
97