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

AbstractMockingStrategy::setMockType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Strategy;
5
6
use Moka\Exception\InvalidArgumentException;
7
use Moka\Exception\MockNotCreatedException;
8
use Moka\Exception\NotImplementedException;
9
use Moka\Stub\StubSet;
10
11
/**
12
 * Class AbstractMockingStrategy
13
 * @package Moka\Strategy
14
 */
15
abstract class AbstractMockingStrategy implements MockingStrategyInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    private $mockType;
21
22
    /**
23
     * @param string $fqcn
24
     * @return object
25
     *
26
     * @throws MockNotCreatedException
27
     */
28 16
    public function build(string $fqcn)
29
    {
30
        try {
31 16
            return $this->doBuild($fqcn);
32 4
        } catch (\Throwable $exception) {
33 4
            throw new MockNotCreatedException(
34 4
                sprintf(
35 4
                    'Cannot create mock object for FQCN %s: %s',
36 4
                    $fqcn,
37 4
                    $exception->getMessage()
38
                )
39
            );
40
        }
41
    }
42
43
    /**
44
     * @param object $mock
45
     * @param StubSet $stubs
46
     * @return void
47
     *
48
     * @throws NotImplementedException
49
     * @throws InvalidArgumentException
50
     */
51 6
    public function decorate($mock, StubSet $stubs)
52
    {
53 6
        $this->checkMockType($mock);
54
55 3
        $this->doDecorate($mock, $stubs);
56
    }
57
58
    /**
59
     * @param object $mock
60
     * @return object
61
     *
62
     * @throws NotImplementedException
63
     * @throws InvalidArgumentException
64
     */
65 14
    public function get($mock)
66
    {
67 14
        $this->checkMockType($mock);
68
69 10
        return $this->doGet($mock);
70
    }
71
72
    /**
73
     * @param string $fqcn
74
     */
75 5
    final protected function setMockType(string $fqcn)
76
    {
77 5
        $this->mockType = $fqcn;
78
    }
79
80
    /**
81
     * @param object $mock
82
     *
83
     * @throws NotImplementedException
84
     * @throws InvalidArgumentException
85
     */
86 17
    final protected function checkMockType($mock)
87
    {
88 17
        if (!$this->mockType) {
89 1
            throw new NotImplementedException();
90
        }
91
92 16
        if (!is_a($mock, $this->mockType)) {
93 6
            throw new InvalidArgumentException(
94 6
                sprintf(
95 6
                    'Mock must be of type %s, %s given',
96 6
                    $this->mockType,
97 6
                    gettype($mock)
98
                )
99
            );
100
        }
101
    }
102
103
    /**
104
     * @param string $fqcn
105
     * @return object
106
     */
107
    abstract protected function doBuild(string $fqcn);
108
109
    /**
110
     * @param object $mock
111
     * @param StubSet $stubs
112
     * @return void
113
     */
114
    abstract protected function doDecorate($mock, StubSet $stubs);
115
116
    /**
117
     * @param object $mock
118
     * @return object
119
     */
120
    abstract protected function doGet($mock);
121
}
122