Report::fromArray()   A
last analyzed

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
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 18
rs 9.4285
c 1
b 0
f 1
ccs 12
cts 12
cp 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 28
    public function toArray() {
13
        return array(
14 28
            'name'  => $this->getName(),
15 28
            'date'  => $this->getDate(),
16 28
            'path'  => $this->getPath(),
17 28
            'items' => $this->getItems(),
18 28
        );
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 32
    public static function fromArray($array = array()) {
28 32
        $required = array('name','date','path','items');
29 32
        $report = new Report();
30
31
        /**
32
         * Check if all required fields are being set.
33
         */
34 32
        foreach ($required as $req) {
35 32
            if (isset($array[$req]) === false) {
36 4
                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 28
        }
39 28
        $report->setName($array['name']);
40 28
        $report->setDate($array['date']);
41 28
        $report->setPath($array['path']);
42 28
        $report->setItems($array['items']);
43 28
        return $report;
44
    }
45
46
}