Completed
Push — master ( b72f37...0c7d03 )
by Alberto
15s
created

AbstractMockingStrategy::verifyMockType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

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 2
eloc 3
nc 2
nop 0
crap 2
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\Stub;
10
use Moka\Stub\StubSet;
11
12
/**
13
 * Class AbstractMockingStrategy
14
 * @package Moka\Strategy
15
 */
16
abstract class AbstractMockingStrategy implements MockingStrategyInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    private $mockType;
22
23
    /**
24
     * @param string $fqcn
25
     * @return object
26
     *
27
     * @throws MockNotCreatedException
28
     */
29 89
    public function build(string $fqcn)
30
    {
31
        try {
32 89
            return $this->doBuild($fqcn);
33 11
        } catch (\Throwable $exception) {
34 11
            throw new MockNotCreatedException(
35 11
                sprintf(
36 11
                    'Cannot create mock object for FQCN "%s": %s',
37 11
                    $fqcn,
38 11
                    $exception->getMessage()
39
                )
40
            );
41
        }
42
    }
43
44
    /**
45
     * @param object $mock
46
     * @param StubSet $stubs
47
     * @return void
48
     *
49
     * @throws NotImplementedException
50
     * @throws InvalidArgumentException
51
     */
52 89
    public function decorate($mock, StubSet $stubs)
53
    {
54 89
        $this->checkMockType($mock);
55
56
        /** @var Stub $stub */
57 89
        foreach ($stubs as $stub) {
58 89
            $this->doDecorate($mock, $stub);
59
        }
60
    }
61
62
    /**
63
     * @param object $mock
64
     * @return object
65
     *
66
     * @throws NotImplementedException
67
     * @throws InvalidArgumentException
68
     */
69 50
    public function get($mock)
70
    {
71 50
        $this->checkMockType($mock);
72
73 46
        return $this->doGet($mock);
74
    }
75
76
    /**
77
     * @return string
78
     *
79
     * @throws NotImplementedException
80
     */
81 23
    public function getMockType(): string
82
    {
83 23
        $this->verifyMockType();
84
85 22
        return $this->mockType;
86
    }
87
88
    /**
89
     * @param string $fqcn
90
     */
91 7
    final protected function setMockType(string $fqcn)
92
    {
93 7
        $this->mockType = $fqcn;
94
    }
95
96
    /**
97
     * @param object $mock
98
     *
99
     * @throws NotImplementedException
100
     * @throws InvalidArgumentException
101
     */
102 89
    final protected function checkMockType($mock)
103
    {
104 89
        $this->verifyMockType();
105
106 89
        if (!is_a($mock, $this->mockType)) {
107 8
            throw new InvalidArgumentException(
108 8
                sprintf(
109 8
                    'Mock must be of type "%s", "%s" given',
110 8
                    $this->mockType,
111 8
                    gettype($mock)
112
                )
113
            );
114
        }
115
    }
116
117
    /**
118
     * @param string $fqcn
119
     * @return object
120
     */
121
    abstract protected function doBuild(string $fqcn);
122
123
    /**
124
     * @param object $mock
125
     * @param Stub $stub
126
     * @return void
127
     */
128
    abstract protected function doDecorate($mock, Stub $stub);
129
130
    /**
131
     * @param object $mock
132
     * @return object
133
     */
134
    abstract protected function doGet($mock);
135
136
    /**
137
     * @return void
138
     *
139
     * @throws NotImplementedException
140
     */
141 90
    private function verifyMockType()
142
    {
143 90
        if (!$this->mockType) {
144 1
            throw new NotImplementedException('Mock type was not defined');
145
        }
146
    }
147
}
148