Completed
Push — master ( 2d5c71...fd1492 )
by Dayle
02:26
created

Experiment::setLaboratory()   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 1
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
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 $laboratory
72
     */
73 21
    public function __construct($name, Laboratory $laboratory)
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
     * Retrieve the laboratory instance.
92
     *
93
     * @return \Scientist\Laboratory|null
94
     */
95
    public function getLaboratory()
96
    {
97 9
        return $this->laboratory;
98
    }
99 9
100
    /**
101 9
     * Register a control callback.
102
     *
103
     * @param callable $callback
104
     *
105
     * @return $this
106
     */
107
    public function control(callable $callback)
108
    {
109 1
        $this->control = $callback;
110
111 1
        return $this;
112
    }
113
114
    /**
115
     * Fetch the control callback.
116
     *
117
     * @return callable
118
     */
119
    public function getControl()
120
    {
121 14
        return $this->control;
122
    }
123 14
124
    /**
125 14
     * Register a trial callback.
126
     *
127
     * @param string   $name
128
     * @param callable $callback
129
     *
130
     * @return $this
131
     */
132
    public function trial($name, callable $callback)
133 13
    {
134
        $this->trials[$name] = $callback;
135 13
136
        return $this;
137
    }
138
139
    /**
140
     * Fetch a trial callback by name.
141
     *
142
     * @param string $name
143
     *
144
     * @return mixed
145
     */
146 12
    public function getTrial($name)
147
    {
148 12
        return $this->trials[$name];
149
    }
150 12
151
    /**
152
     * Fetch an array of trial callbacks.
153
     *
154
     * @return array
155
     */
156
    public function getTrials()
157
    {
158
        return $this->trials;
159
    }
160 2
161
    /**
162 2
     * Set a matcher for this experiment.
163
     *
164
     * @param \Scientist\Matchers\Matcher $matcher
165
     *
166
     * @return $this
167
     */
168
    public function matcher(Matcher $matcher)
169
    {
170 11
        $this->matcher = $matcher;
171
172 11
        return $this;
173
    }
174
175
    /**
176
     * Get the matcher for this experiment.
177
     *
178
     * @return \Scientist\Matchers\Matcher
179
     */
180
    public function getMatcher()
181
    {
182 1
        return $this->matcher;
183
    }
184 1
185
    /**
186 1
     * Set the execution chance.
187
     *
188
     * @param integer $chance
189
     *
190
     * @return $this
191
     */
192
    public function chance($chance)
193
    {
194 11
        $this->chance = (int) $chance;
195
196 11
        return $this;
197
    }
198
199
    /**
200
     * Get the execution chance.
201
     *
202
     * @return integer
203
     */
204
    public function getChance()
205
    {
206 2
        return $this->chance;
207
    }
208 2
209
    /**
210 2
     * Determine whether an experiment should run based on chance.
211
     *
212
     * @return boolean
213
     */
214
    public function shouldRun()
215
    {
216
        return rand(0, 100) <= $this->chance;
217
    }
218 1
219
    /**
220 1
     * Get the experiment parameters.
221
     *
222
     * @return array
223
     */
224
    public function getParams()
225
    {
226
        return $this->params;
227
    }
228 5
229
    /**
230 5
     * Execute the experiment within the laboratory.
231
     *
232
     * @return mixed
233
     */
234
    public function run()
235
    {
236
        $this->params = func_get_args();
237
238 12
        return $this->laboratory->runExperiment($this);
239
    }
240 12
241
    /**
242
     * Execute the experiment and return a report.
243
     *
244
     * @return \Scientist\Report
245
     */
246
    public function report()
247
    {
248 6
        $this->params = func_get_args();
249
250 6
        return $this->laboratory->getReport($this);
251
    }
252
}
253