Passed
Push — master ( 9ac841...1b65d4 )
by Dayle
02:12
created

Laboratory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 109
ccs 27
cts 27
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setJournals() 0 6 1
A addJournal() 0 6 1
A getJournals() 0 4 1
A experiment() 0 4 1
A getResult() 0 7 1
A reportToJournals() 0 6 2
A runExperiment() 0 12 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
    /**
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 5
    public function experiment($name)
70
    {
71 5
        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 4
    public function runExperiment(Experiment $experiment)
82
    {
83 4
        if ($experiment->shouldRun()) {
84 3
            $result = $this->getResult($experiment);
85 3
            return $result->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 4
    public function getResult(Experiment $experiment)
102
    {
103 4
        $result = (new Intern)->run($experiment);
104 4
        $this->reportToJournals($experiment, $result);
105
106 4
        return $result;
107
    }
108
109
    /**
110
     * Report experiment result to registered journals.
111
     *
112
     * @param \Scientist\Experiment $experiment
113
     * @param \Scientist\Report     $result
114
     *
115
     * @return void
116
     */
117 4
    protected function reportToJournals(Experiment $experiment, Report $result)
118
    {
119 4
        foreach ($this->journals as $journal) {
120 2
            $journal->report($experiment, $result);
121 4
        }
122 4
    }
123
}
124