Passed
Push — master ( 43cce1...1a30b3 )
by Fran
02:56
created

JsonResponse::parseData()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.7085

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 4
cts 7
cp 0.5714
crap 3.7085
rs 10
1
<?php
2
3
namespace PSFS\base\dto;
4
5
class JsonResponse extends Dto
6
{
7
    /**
8
     * Response message
9
     * @var string $message
10
     */
11
    public $message = 'No message';
12
    /**
13
     * Response result
14
     * @var bool $success
15
     */
16
    public $success = false;
17
    /**
18
     * Data of response
19
     * @var array $data
20
     */
21
    public $data = null;
22
    /**
23
     * Number of total results
24
     * @var int total
25
     */
26
    public $total = 0;
27
    /**
28
     * Number of pages availables
29
     * @var int pages
30
     */
31
    public $pages = 1;
32
33 1
    private function parseData($data) {
34 1
        if ($data instanceof \Generator) {
35
            $generatedData = [];
36
            foreach($data as $datum) {
37
                $generatedData[] = $datum;
38
            }
39
        } else {
40 1
            $generatedData = $data;
41
        }
42 1
        return $generatedData;
43
    }
44
45
    /**
46
     * JsonResponse constructor.
47
     * @param array $data
48
     * @param bool $result
49
     * @param integer $total
50
     * @param int $pages
51
     * @param string $message
52
     * @throws \PSFS\base\exception\GeneratorException
53
     */
54 1
    public function __construct($data = [], $result = false, $total = null, $pages = 1, $message = null)
55
    {
56 1
        parent::__construct(false);
57 1
        $this->data = $this->parseData($data);
58 1
        $this->success = $result;
59 1
        $this->total = $total ?: (is_array($this->data) ? count($this->data) : ($total ?? 0));
0 ignored issues
show
introduced by
The condition is_array($this->data) is always true.
Loading history...
60 1
        $this->pages = $pages;
61 1
        if (null !== $message) {
62 1
            $this->message = $message;
63
        }
64
    }
65
}
66