Completed
Pull Request — master (#12)
by Angelo
08:49
created

AbstractMockingStrategy::verifyMockType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 6
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
     * @return string
74
     *
75 5
     * @throws NotImplementedException
76
     */
77 5
    public function getMockType(): string
78
    {
79
        $this->verifyMockType();
80
81
        return $this->mockType;
82
    }
83
84
    /**
85
     * @param string $fqcn
86 17
     */
87
    final protected function setMockType(string $fqcn)
88 17
    {
89 1
        $this->mockType = $fqcn;
90
    }
91
92 16
    /**
93 6
     * @param object $mock
94 6
     *
95 6
     * @throws NotImplementedException
96 6
     * @throws InvalidArgumentException
97 6
     */
98
    final protected function checkMockType($mock)
99
    {
100
        $this->verifyMockType();
101
102
        if (!is_a($mock, $this->mockType)) {
103
            throw new InvalidArgumentException(
104
                sprintf(
105
                    'Mock must be of type "%s", "%s" given',
106
                    $this->mockType,
107
                    gettype($mock)
108
                )
109
            );
110
        }
111
    }
112
113
    /**
114
     * @param string $fqcn
115
     * @return object
116
     */
117
    abstract protected function doBuild(string $fqcn);
118
119
    /**
120
     * @param object $mock
121
     * @param StubSet $stubs
122
     * @return void
123
     */
124
    abstract protected function doDecorate($mock, StubSet $stubs);
125
126
    /**
127
     * @param object $mock
128
     * @return object
129
     */
130
    abstract protected function doGet($mock);
131
132
    /**
133
     * @return void
134
     *
135
     * @throws NotImplementedException
136
     */
137
    private function verifyMockType()
138
    {
139
        if (!$this->mockType) {
140
            throw new NotImplementedException('Mock type was not defined');
141
        }
142
    }
143
}
144