Completed
Push — release/1.0 ( 82bfa1...74ea79 )
by Johnny
512:54 queued 510:54
created

Report::fromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
ccs 0
cts 0
cp 0
rs 9.4286
cc 3
eloc 11
nc 3
nop 1
crap 12
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 4
     */
12
    public function toArray() {
13
        return array(
14 4
            'name'  => $this->getName(),
15 4
            'date'  => $this->getDate(),
16 4
            'path'  => $this->getPath(),
17 4
            'items' => $this->getItems(),
18
        );
19 2
    }
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
    static function fromArray($array = array()) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
28
        $required = array('name','date','path','items');
29
        $report = new Report();
30
31
        /**
32
         * Check if all required fields are being set.
33
         */
34
        foreach ($required as $req) {
35
            if (isset($array[$req]) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
36
                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
        }
39
        $report->setName($array['name']);
40
        $report->setDate($array['date']);
41
        $report->setPath($array['path']);
42
        $report->setItems($array['items']);
43
        return $report;
44
    }
45
46
}