Completed
Pull Request — master (#12)
by Mauro
02:28
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 2
Bugs 0 Features 0
Metric Value
c 2
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 \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 8
    public function experiment($name)
73
    {
74 8
        return (new Experiment($name, $this));
75
    }
76
77
    /**
78
     * Run an experiment.
79
     *
80
     * @param \Scientist\Experiment $experiment
81
     *
82
     * @return mixed
83
     */
84 6
    public function runExperiment(Experiment $experiment)
85
    {
86 6
        if ($experiment->shouldRun()) {
87 5
            $report = $this->getReport($experiment);
88 4
            if (is_array($report)) {
89
                $values = [];
90
                foreach ($report as $item) {
91
                    $values[] = $item->getControl()->getValue();
92
                }
93
94
                return implode(',', $values);
95
            } else {
96 4
                return $report->getControl()->getValue();
97
            }
98
        }
99
100 1
        return call_user_func_array(
101 1
            $experiment->getControl(),
102 1
            $experiment->getParams()
103 1
        );
104
    }
105
106
    /**
107
     * Run an experiment and return the result.
108
     *
109
     * @param \Scientist\Experiment $experiment
110
     *
111
     * @return \Scientist\Report
112
     */
113 10
    public function getReport(Experiment $experiment)
114
    {
115 10
        $report = (new Intern)->run($experiment);
0 ignored issues
show
Bug Compatibility introduced by
The expression (new \Scientist\Intern())->run($experiment); of type array|Scientist\Report adds the type array to the return on line 118 which is incompatible with the return type documented by Scientist\Laboratory::getReport of type Scientist\Report.
Loading history...
116 9
        $this->reportToJournals($experiment, $report);
117
118 9
        return $report;
119
    }
120
121
    /**
122
     * Report experiment result to registered journals.
123
     *
124
     * @param \Scientist\Experiment   $experiment
125
     * @param \Scientist\Report|array $report
126
     *
127
     * @return void
128
     */
129 9
    protected function reportToJournals(Experiment $experiment, $report)
130
    {
131 9
        foreach ($this->journals as $journal) {
132 2
            if (is_array($report)) {
133
                foreach ($report as $item) {
134
                    $journal->report($experiment, $item);
135
                }
136
            } else {
137 2
                $journal->report($experiment, $report);
138
            }
139 9
        }
140 9
    }
141
}
142