Call::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Mock;
6
7
class Call
8
{
9
    /**
10
     * @var string
11
     */
12
    private $method;
13
14
    /**
15
     * @var bool
16
     */
17
    private $hasWith = false;
18
19
    /**
20
     * @var array
21
     */
22
    private $with = [];
23
24
    /**
25
     * @var \Throwable|null
26
     */
27
    private $exception;
28
29
    /**
30
     * @var bool
31
     */
32
    private $hasReturnSelf = false;
33
34
    /**
35
     * @var bool
36
     */
37
    private $hasReturn = false;
38
39
    /**
40
     * @var bool
41
     */
42
    private $hasReturnCallback = false;
43
44
    /**
45
     * @var mixed
46
     */
47
    private $return;
48
49
    /**
50
     * @var callable|null
51
     */
52
    private $returnCallback;
53
54 19
    private function __construct()
55
    {
56 19
    }
57
58
    public static function create(string $method): self
59
    {
60
        $self = new self();
61
        $self->method = $method;
62
63 19
        return $self;
64
    }
65 19
66 19
    /**
67
     * @param mixed ...$with
68 19
     */
69
    public function with(...$with): self
70
    {
71
        $this->hasWith = true;
72
        $this->with = $with;
73
74
        return $this;
75
    }
76 2
77
    public function willThrowException(\Throwable $exception): self
78 2
    {
79 2
        if ($this->hasReturnSelf) {
80
            throw new \InvalidArgumentException(sprintf('%s: There is already a return self', __METHOD__));
81 2
        }
82
83
        if ($this->hasReturn) {
84
            throw new \InvalidArgumentException(sprintf('%s: There is already a return', __METHOD__));
85
        }
86
87
        if ($this->hasReturnCallback) {
88
            throw new \InvalidArgumentException(sprintf('%s: There is already a return callback', __METHOD__));
89 8
        }
90
91 8
        $this->exception = $exception;
92 1
93
        return $this;
94
    }
95 7
96 1
    public function willReturnSelf(): self
97
    {
98
        if (null !== $this->exception) {
99 6
            throw new \InvalidArgumentException(sprintf('%s: There is already a exception', __METHOD__));
100 1
        }
101
102
        if ($this->hasReturn) {
103 5
            throw new \InvalidArgumentException(sprintf('%s: There is already a return', __METHOD__));
104
        }
105 5
106
        if ($this->hasReturnCallback) {
107
            throw new \InvalidArgumentException(sprintf('%s: There is already a return callback', __METHOD__));
108
        }
109
110
        $this->hasReturnSelf = true;
111 7
112
        return $this;
113 7
    }
114 1
115
    /**
116
     * @param mixed $return
117 6
     */
118 1
    public function willReturn($return): self
119
    {
120
        if (null !== $this->exception) {
121 5
            throw new \InvalidArgumentException(sprintf('%s: There is already a exception', __METHOD__));
122 1
        }
123
124
        if ($this->hasReturnSelf) {
125 4
            throw new \InvalidArgumentException(sprintf('%s: There is already a return self', __METHOD__));
126
        }
127 4
128
        if ($this->hasReturnCallback) {
129
            throw new \InvalidArgumentException(sprintf('%s: There is already a return callback', __METHOD__));
130
        }
131
132
        $this->hasReturn = true;
133
        $this->return = $return;
134
135 7
        return $this;
136
    }
137 7
138 1
    public function willReturnCallback(callable $returnCallback): self
139
    {
140
        if (null !== $this->exception) {
141 6
            throw new \InvalidArgumentException(sprintf('%s: There is already a exception', __METHOD__));
142 1
        }
143
144
        if ($this->hasReturnSelf) {
145 5
            throw new \InvalidArgumentException(sprintf('%s: There is already a return self', __METHOD__));
146 1
        }
147
148
        if ($this->hasReturn) {
149 4
            throw new \InvalidArgumentException(sprintf('%s: There is already a return', __METHOD__));
150 4
        }
151
152 4
        $this->hasReturnCallback = true;
153
        $this->returnCallback = $returnCallback;
154
155
        return $this;
156
    }
157
158 7
    public function getMethod(): string
159
    {
160 7
        return $this->method;
161 1
    }
162
163
    public function hasWith(): bool
164 6
    {
165 1
        return $this->hasWith;
166
    }
167
168 5
    public function hasReturnSelf(): bool
169 1
    {
170
        return $this->hasReturnSelf;
171
    }
172 4
173 4
    public function hasReturn(): bool
174
    {
175 4
        return $this->hasReturn;
176
    }
177
178
    public function hasReturnCallback(): bool
179
    {
180
        return $this->hasReturnCallback;
181 7
    }
182
183 7
    public function getWith(): array
184
    {
185
        return $this->with;
186
    }
187
188
    /**
189 7
     * @return \Throwable|null
190
     */
191 7
    public function getException()
192
    {
193
        return $this->exception;
194
    }
195
196
    /**
197 7
     * @return mixed
198
     */
199 7
    public function getReturn()
200
    {
201
        return $this->return;
202
    }
203
204
    /**
205 7
     * @return mixed
206
     */
207 7
    public function getReturnCallback()
208
    {
209
        return $this->returnCallback;
210
    }
211
}
212