Completed
Pull Request — master (#14)
by Steve
02:24
created

Laboratory::experiment()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
    /**
18
     * Collection of journals to report to.
19
     *
20
     * @var \Scientist\Journals\Journal[]
21
     */
22
    protected $journals = [];
23
24
    /**
25
     * Register a collection of journals.
26
     *
27
     * @param array $journals
28
     *
29
     * @return $this
30
     */
31 1
    public function setJournals(array $journals = [])
32
    {
33 1
        $this->journals = [];
34 1
        foreach ($journals as $journal) {
35 1
            $this->addJournal($journal);
36 1
        }
37
38 1
        return $this;
39
    }
40
41
    /**
42
     * Register a new journal.
43
     *
44
     * @param \Scientist\Journals\Journal $journal
45
     *
46
     * @return $this
47
     */
48 5
    public function addJournal(Journal $journal)
49
    {
50 5
        $this->journals[] = $journal;
51
52 5
        return $this;
53
    }
54
55
    /**
56
     * Retrieve registers journals.
57
     *
58
     * @return array
59
     */
60 3
    public function getJournals()
61
    {
62 3
        return $this->journals;
63
    }
64
65
    /**
66
     * Start a new experiment.
67
     *
68
     * @param string $name
69
     *
70
     * @return mixed
71
     */
72 7
    public function experiment($name)
73
    {
74 7
        return (new Experiment($name, $this));
75
    }
76
77
    /**
78
     * Run an experiment.
79
     *
80
     * @param \Scientist\Experiment $experiment
81
     *
82
     * @return mixed
83
     */
84 5
    public function runExperiment(Experiment $experiment)
85
    {
86 5
        if ($experiment->shouldRun()) {
87 4
            $report = $this->createReport($experiment);
88 4
            return $report->getControl()->getValue();
89
        }
90
91 1
        return call_user_func_array(
92 1
            $experiment->getControl(),
93 1
            $experiment->getParams()
94 1
        );
95
    }
96
97
    /**
98
     * Run an experiment and return the result.
99
     *
100
     * @param \Scientist\Experiment $experiment
101
     *
102
     * @return \Scientist\Report
103
     */
104 7
    public function createReport(Experiment $experiment)
105
    {
106 7
        $report = (new Intern)->run($experiment);
107 7
        $this->reportToJournals($experiment, $report);
108
109 7
        return $report;
110
    }
111
112
    /**
113
     * Report experiment result to registered journals.
114
     *
115
     * @param \Scientist\Experiment $experiment
116
     * @param \Scientist\Report     $report
117
     *
118
     * @return void
119
     */
120 7
    protected function reportToJournals(Experiment $experiment, Report $report)
121
    {
122 7
        foreach ($this->journals as $journal) {
123 2
            $journal->report($experiment, $report);
124 7
        }
125 7
    }
126
}
127