Completed
Pull Request — master (#37)
by Angelo
02:34
created

ProxyTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 58.33%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 57
ccs 7
cts 12
cp 0.5833
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __call() 0 6 2
A _moka_setObject() 0 4 1
A _moka_setMockingStrategy() 0 4 1
A stub() 0 6 1
A serve() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Generator;
5
6
7
use Moka\Exception\InvalidArgumentException;
8
use Moka\Exception\MockNotCreatedException;
9
use Moka\Exception\MockNotServedException;
10
use Moka\Proxy\ProxyInterface;
11
use Moka\Strategy\MockingStrategyInterface;
12
13
trait ProxyTrait
14
{
15
    private $object;
16
17
    /**
18
     * @var MockingStrategyInterface
19
     */
20
    private $mockingStrategy;
21
22
    public function __construct()
23
    {
24
    }
25
26 1
    public function __call($name, array $arguments)
27
    {
28 1
        if ($this->mockingStrategy instanceof MockingStrategyInterface) {
29 1
            return $this->mockingStrategy->call($this->object, $name, $arguments);
30
        }
31
    }
32
33
    /**
34
     * @param mixed $object
35
     */
36 8
    public function _moka_setObject($object)
37
    {
38 8
        $this->object = $object;
39
    }
40
41 8
    public function _moka_setMockingStrategy(MockingStrategyInterface $mockingStrategy)
42
    {
43 8
        $this->mockingStrategy = $mockingStrategy;
44
    }
45
46
    /**
47
     * @param array $methodsWithValues
48
     * @return ProxyInterface
49
     *
50
     * @throws InvalidArgumentException
51
     * @throws MockNotCreatedException
52
     */
53
    public function stub(array $methodsWithValues): ProxyInterface
54
    {
55
        $this->mockingStrategy->decorate($this->object, $methodsWithValues);
56
57
        return $this;
58
    }
59
60
    /**
61
     * @return object
62
     *
63
     * @throws MockNotServedException
64
     */
65
    public function serve()
66
    {
67
        return $this->object;
68
    }
69
}
70