Completed
Push — master ( 51895c...7790b0 )
by Alberto
15s
created

AbstractMockingStrategy::decorate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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 40
    public function build(string $fqcn)
29
    {
30
        try {
31 40
            return $this->doBuild($fqcn);
32 6
        } catch (\Throwable $exception) {
33 6
            throw new MockNotCreatedException(
34 6
                sprintf(
35 6
                    'Cannot create mock object for FQCN "%s": %s',
36 6
                    $fqcn,
37 6
                    $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 9
    public function decorate($mock, StubSet $stubs)
52
    {
53 9
        $this->checkMockType($mock);
54
55 6
        $this->doDecorate($mock, $stubs);
56
    }
57
58
    /**
59
     * @param object $mock
60
     * @return object
61
     *
62
     * @throws NotImplementedException
63
     * @throws InvalidArgumentException
64
     */
65 23
    public function get($mock)
66
    {
67 23
        $this->checkMockType($mock);
68
69 20
        return $this->doGet($mock);
70
    }
71
72
    /**
73
     * @return string
74
     *
75
     * @throws NotImplementedException
76
     */
77 19
    public function getMockType(): string
78
    {
79 19
        $this->verifyMockType();
80
81 18
        return $this->mockType;
82
    }
83
84
    /**
85
     * @param string $fqcn
86
     */
87 5
    final protected function setMockType(string $fqcn)
88
    {
89 5
        $this->mockType = $fqcn;
90
    }
91
92
    /**
93
     * @param object $mock
94
     *
95
     * @throws NotImplementedException
96
     * @throws InvalidArgumentException
97
     */
98 26
    final protected function checkMockType($mock)
99
    {
100 26
        $this->verifyMockType();
101
102 26
        if (!is_a($mock, $this->mockType)) {
103 6
            throw new InvalidArgumentException(
104 6
                sprintf(
105 6
                    'Mock must be of type "%s", "%s" given',
106 6
                    $this->mockType,
107 6
                    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 45
    private function verifyMockType()
138
    {
139 45
        if (!$this->mockType) {
140 1
            throw new NotImplementedException('Mock type was not defined');
141
        }
142
    }
143
}
144