Completed
Pull Request — master (#7)
by Jeremy
03:39 queued 01:25
created

Laboratory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 11
c 5
b 0
f 1
lcom 1
cbo 4
dl 0
loc 127
ccs 34
cts 34
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A stopTrialsEarly() 0 5 1
A setJournals() 0 6 1
A addJournal() 0 6 1
A getJournals() 0 4 1
A experiment() 0 4 1
A runExperiment() 0 12 2
A getReport() 0 12 2
A reportToJournals() 0 6 2
1
<?php
2
3
namespace Scientist;
4
5
use Scientist\Journals\Journal;
6
7
/**
8
 * Class Laboratory
9
 *
10
 * The Laboratory is where the magic takes place. Here we define
11
 * and conduct our experiments.
12
 *
13
 * @package \Scientist
14
 */
15
class Laboratory
16
{
17
    /** @var bool  */
18
    protected $stopTrialsEarly = false;
19
20
    /**
21
     * Collection of journals to report to.
22
     *
23
     * @var array
24
     */
25
    protected $journals = [];
26
27
    /**
28
     * Experiments wont hide exceptions.
29
     * @return $this
30
     */
31 1
    public function stopTrialsEarly()
32
    {
33 1
        $this->stopTrialsEarly = true;
34 1
        return $this;
35
    }
36
37
    /**
38
     * Register a collection of journals.
39
     *
40
     * @param array $journals
41
     *
42
     * @return $this
43
     */
44 1
    public function setJournals(array $journals = [])
45
    {
46 1
        $this->journals = $journals;
47
48 1
        return $this;
49
    }
50
51
    /**
52
     * Register a new journal.
53
     *
54
     * @param \Scientist\Journals\Journal $journal
55
     *
56
     * @return $this
57
     */
58 4
    public function addJournal(Journal $journal)
59
    {
60 4
        $this->journals[] = $journal;
61
62 4
        return $this;
63
    }
64
65
    /**
66
     * Retrieve registers journals.
67
     *
68
     * @return array
69
     */
70 3
    public function getJournals()
71
    {
72 3
        return $this->journals;
73
    }
74
75
    /**
76
     * Start a new experiment.
77
     *
78
     * @param string $name
79
     *
80
     * @return mixed
81
     */
82 9
    public function experiment($name)
83
    {
84 9
        return (new Experiment($name))->setLaboratory($this);
85
    }
86
87
    /**
88
     * Run an experiment.
89
     *
90
     * @param \Scientist\Experiment $experiment
91
     *
92
     * @return mixed
93
     */
94 6
    public function runExperiment(Experiment $experiment)
95
    {
96 6
        if ($experiment->shouldRun()) {
97 5
            $report = $this->getReport($experiment);
98 3
            return $report->getControl()->getValue();
99
        }
100
101 1
        return call_user_func_array(
102 1
            $experiment->getControl(),
103 1
            $experiment->getParams()
104 1
        );
105
    }
106
107
    /**
108
     * Run an experiment and return the result.
109
     *
110
     * @param \Scientist\Experiment $experiment
111
     *
112
     * @return \Scientist\Report
113
     */
114 8
    public function getReport(Experiment $experiment)
115
    {
116 8
        $intern = new Intern;
117 8
        if ($this->stopTrialsEarly) {
118 1
            $intern->overreact();
119 1
        }
120
121 8
        $report = $intern->run($experiment);
122 6
        $this->reportToJournals($experiment, $report);
123
124 6
        return $report;
125
    }
126
127
    /**
128
     * Report experiment result to registered journals.
129
     *
130
     * @param \Scientist\Experiment $experiment
131
     * @param \Scientist\Report     $report
132
     *
133
     * @return void
134
     */
135 6
    protected function reportToJournals(Experiment $experiment, Report $report)
136
    {
137 6
        foreach ($this->journals as $journal) {
138 2
            $journal->report($experiment, $report);
139 6
        }
140 6
    }
141
}
142