Completed
Push — master ( db6d89...ab9aea )
by Dayle
02:18
created

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