JsonModelWriter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 35
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getData() 0 27 4
1
<?php
2
3
/*
4
 * soluble-flexstore library
5
 *
6
 * @author    Vanvelthem Sébastien
7
 * @link      https://github.com/belgattitude/soluble-flexstore
8
 * @copyright Copyright (c) 2016-2017 Vanvelthem Sébastien
9
 * @license   MIT License https://github.com/belgattitude/soluble-flexstore/blob/master/LICENSE.md
10
 *
11
 */
12
13
namespace Soluble\FlexStore\Writer\Zend;
14
15
use Zend\View\Model\JsonModel as ZendJsonModel;
16
use Soluble\FlexStore\Source\QueryableSourceInterface;
17
use Soluble\FlexStore\Options;
18
use Soluble\FlexStore\Writer\AbstractWriter;
19
20
class JsonModelWriter extends AbstractWriter
21
{
22
    /**
23
     * @param Options $options
24
     *
25
     * @return ZendJsonModel
26
     */
27
    public function getData(Options $options = null)
28
    {
29
        if ($options === null) {
30
            // Take store global/default options
31
            $options = $this->store->getOptions();
32
        }
33
34
        $data = $this->store->getData($options);
35
        $d = [
36
            'success' => true,
37
            'total' => $data->getTotalRows(),
38
            'start' => $data->getSource()->getOptions()->getOffset(),
39
            'limit' => $data->getSource()->getOptions()->getLimit(),
40
            'data' => $data->toArray()
41
        ];
42
43
        if ($this->options['debug']) {
44
            $source = $data->getSource();
45
            if ($source instanceof QueryableSourceInterface) {
46
                $d['query'] = $source->getQueryString();
47
            }
48
        }
49
50
        $json = new ZendJsonModel($d);
51
52
        return $json;
53
    }
54
}
55