Test Setup Failed
Push — master ( d89a8a...fb45e4 )
by Dayle
45s
created

Experiment::getControl()   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 0
crap 1
1
<?php
2
3
namespace Scientist;
4
5
use Scientist\Chances\Chance;
6
use Scientist\Chances\StandardChance;
7
use Scientist\Matchers\Matcher;
8
use Scientist\Matchers\StandardMatcher;
9
10
/**
11
 * Class Experiment
12
 *
13
 * An experiment allows us to implement our code in a new way without
14
 * risking the introduction of bugs or regressions.
15
 *
16
 * @package Scientist
17
 */
18
class Experiment
19
{
20
    /**
21
     * Experiment name.
22
     *
23
     * @var string
24
     */
25
    protected $name;
26
27
    /**
28
     * The control callback.
29
     *
30
     * @var callable
31
     */
32
    protected $control;
33
34
    /**
35
     * Context for the control.
36
     *
37
     * @var mixed
38
     */
39
    protected $controlContext;
40
41
    /**
42
     * Trial callbacks.
43
     *
44
     * @var array
45
     */
46
    protected $trials = [];
47
48
    /**
49
     * Parameters for our callbacks.
50
     *
51
     * @var array
52
     */
53
    protected $params = [];
54
55
    /**
56
     * Laboratory instance.
57
     *
58
     * @var \Scientist\Laboratory
59
     */
60
    protected $laboratory;
61
62
    /**
63
     * Matcher for experiment values.
64
     *
65
     * @var \Scientist\Matchers\Matcher
66
     */
67
    protected $matcher;
68
69
    /**
70
     * Execution chance.
71
     *
72
     * @var \Scientist\Chances\Chance
73 21
     */
74
    protected $chance;
75 21
76 21
    /**
77 21
     * Create a new experiment.
78 21
     *
79
     * @param string                $name
80
     * @param \Scientist\Laboratory $laboratory
81
     */
82
    public function __construct($name, Laboratory $laboratory)
83
    {
84
        $this->name = $name;
85 12
        $this->laboratory = $laboratory;
86
        $this->matcher = new StandardMatcher;
87 12
        $this->chance = new StandardChance;
88
    }
89
90
    /**
91
     * Fetch the experiment name.
92
     *
93
     * @return string
94
     */
95 1
    public function getName()
96
    {
97 1
        return $this->name;
98
    }
99
100
    /**
101
     * Retrieve the laboratory instance.
102
     *
103
     * @return \Scientist\Laboratory|null
104
     */
105
    public function getLaboratory()
106
    {
107 14
        return $this->laboratory;
108
    }
109 14
110
    /**
111 14
     * Register a control callback.
112
     *
113
     * @param callable $callback
114
     * @param mixed $context
115
     *
116
     * @return $this
117
     */
118
    public function control(callable $callback, $context = null)
119 14
    {
120
        $this->control = $callback;
121 14
        $this->controlContext = $context;
122
123
        return $this;
124
    }
125
126
    /**
127
     * Fetch the control callback.
128
     *
129
     * @return callable
130
     */
131
    public function getControl()
132 12
    {
133
        return $this->control;
134 12
    }
135
136 12
    public function getControlContext()
137
    {
138
        return $this->controlContext;
139
    }
140
141
    /**
142
     * Register a trial callback.
143
     *
144
     * @param string   $name
145
     * @param callable $callback
146 2
     *
147
     * @return $this
148 2
     */
149
    public function trial($name, callable $callback, $context = null)
150
    {
151
        $this->trials[$name] = new Trial($name, $callback, $context);
152
153
        return $this;
154
    }
155
156 12
    /**
157
     * Fetch a trial callback by name.
158 12
     *
159
     * @param string $name
160
     *
161
     * @return mixed
162
     */
163
    public function getTrial($name)
164
    {
165
        return $this->trials[$name]->getCallback();
166
    }
167
168 1
    /**
169
     * Fetch an array of trial callbacks.
170 1
     *
171
     * @return array
172 1
     */
173
    public function getTrials()
174
    {
175
        return $this->trials;
176
    }
177
178
    /**
179
     * Set a matcher for this experiment.
180 12
     *
181
     * @param \Scientist\Matchers\Matcher $matcher
182 12
     *
183
     * @return $this
184
     */
185
    public function matcher(Matcher $matcher)
186
    {
187
        $this->matcher = $matcher;
188
189
        return $this;
190
    }
191
192 2
    /**
193
     * Get the matcher for this experiment.
194 2
     *
195
     * @return \Scientist\Matchers\Matcher
196 2
     */
197
    public function getMatcher()
198
    {
199
        return $this->matcher;
200
    }
201
202
    /**
203
     * Set the execution chance.
204 1
     *
205
     * @param Chances\Chance $chance
206 1
     *
207
     * @return $this
208
     */
209
    public function chance(Chance $chance)
210
    {
211
        $this->chance = $chance;
212
213
        return $this;
214 6
    }
215
216 6
    /**
217
     * Get the execution chance.
218
     *
219
     * @return Chances\Chance
220
     */
221
    public function getChance()
222
    {
223
        return $this->chance;
224 13
    }
225
226 13
    /**
227
     * Determine whether an experiment should run based on chance.
228
     *
229
     * @return boolean
230
     */
231
    public function shouldRun()
232
    {
233
        return $this->chance
234 6
            ->shouldRun();
235
    }
236 6
237
    /**
238 6
     * Get the experiment parameters.
239
     *
240
     * @return array
241
     */
242
    public function getParams()
243
    {
244
        return $this->params;
245
    }
246 3
247
    /**
248 3
     * Execute the experiment within the laboratory.
249
     *
250 3
     * @return mixed
251
     */
252
    public function run()
253
    {
254
        $this->params = func_get_args();
255
256
        return $this->laboratory->runExperiment($this);
257
    }
258
259
    /**
260
     * Execute the experiment and return a report.
261
     *
262
     * @return \Scientist\Report
263
     */
264
    public function report()
265
    {
266
        $this->params = func_get_args();
267
268
        return $this->laboratory->getReport($this);
269
    }
270
}
271