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

Laboratory::getReport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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