Completed
Push — master ( 934cdf...8f45fc )
by Johnny
01:43
created

Report::fromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.4286
cc 3
eloc 11
nc 3
nop 1
crap 3
1
<?php
2
namespace Redbox\Scan\Report;
3
use Redbox\Scan\Exception;
4
5
class Report extends AbstractReport
6
{
7
    /**
8
     * Return the report in array form.
9
     *
10
     * @return array
11
     */
12 16
    public function toArray() {
13
        return array(
14 16
            'name'  => $this->getName(),
15 16
            'date'  => $this->getDate(),
16 16
            'path'  => $this->getPath(),
17 16
            'items' => $this->getItems(),
18 8
        );
19
    }
20
21
    /**
22
     * Return an instance of Report by providing an array to the method.
23
     *
24
     * @param array $array
25
     * @return Report
26
     */
27 16
    public static function fromArray($array = array()) {
28 16
        $required = array('name','date','path','items');
29 16
        $report = new Report();
30
31
        /**
32
         * Check if all required fields are being set.
33
         */
34 16
        foreach ($required as $req) {
35 16
            if (isset($array[$req]) === false) {
36 10
                throw new Exception\RuntimeException('Could not create a report from this array field ' . $req . ' was not set the following fields are required (' . implode(',', $required) . ')');
37
            }
38 6
        }
39 12
        $report->setName($array['name']);
40 12
        $report->setDate($array['date']);
41 12
        $report->setPath($array['path']);
42 12
        $report->setItems($array['items']);
43 12
        return $report;
44
    }
45
46
}