Completed
Push — master ( 9bde92...3d27da )
by Nils
03:57
created

Datatable19Version   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 6
c 6
b 0
f 1
lcom 1
cbo 7
dl 0
loc 73
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A canParseRequest() 0 4 1
A parseRequest() 0 4 1
A createResponse() 0 10 1
A getTableView() 0 4 1
A getScriptView() 0 4 1
1
<?php
2
3
namespace OpenSkill\Datatable\Versions;
4
5
6
use OpenSkill\Datatable\Columns\ColumnConfiguration;
7
use OpenSkill\Datatable\Data\ResponseData;
8
use OpenSkill\Datatable\Queries\Parser\Datatable19QueryParser;
9
use OpenSkill\Datatable\Queries\QueryConfiguration;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
use Symfony\Component\HttpFoundation\RequestStack;
12
13
/**
14
 * Class Datatable19Version
15
 * @package OpenSkill\Datatable\Versions
16
 *
17
 * Version that supports the 1.9 version of datatables
18
 * http://legacy.datatables.net/index
19
 *
20
 */
21
class Datatable19Version extends Version
22
{
23
    /** @var Datatable19QueryParser */
24
    private $queryParser;
25
26
    /**
27
     * Datatable19Version constructor.
28
     *
29
     * @param RequestStack $requestStack The current request
30
     */
31
    public function __construct(RequestStack $requestStack)
32
    {
33
        parent::__construct($requestStack);
34
        $this->queryParser = new Datatable19QueryParser();
35
    }
36
37
    /**
38
     * Method to determine if this parser can handle the query parameters. If so then the parser should return true
39
     * and be able to return a DTQueryConfiguration
40
     *
41
     * @return bool true if the parser is able to parse the query parameters and to return a DTQueryConfiguration
42
     */
43
    public function canParseRequest()
44
    {
45
        return $this->queryParser->canParse($this->requestStack->getCurrentRequest());
0 ignored issues
show
Bug introduced by
It seems like $this->requestStack->getCurrentRequest() can be null; however, canParse() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
46
    }
47
48
    /**
49
     * Method that should parse the request and return a DTQueryConfiguration
50
     *
51
     * @param ColumnConfiguration[] $columnConfiguration The configuration of the columns
52
     *
53
     * @return QueryConfiguration the configuration the provider can use to prepare the data
54
     */
55
    public function parseRequest(array $columnConfiguration)
56
    {
57
        return $this->queryParser->parse($this->requestStack->getCurrentRequest(), $columnConfiguration);
0 ignored issues
show
Bug introduced by
It seems like $this->requestStack->getCurrentRequest() can be null; however, parse() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
58
    }
59
60
    /**
61
     * Is responsible to take the generated data and prepare a response for it.
62
     * @param ResponseData $data The processed data.
63
     * @param QueryConfiguration $queryConfiguration the query configuration for the current request.
64
     * @param ColumnConfiguration[] $columnConfigurations the column configurations for the current data table.
65
     * @return JsonResponse the response that should be returned to the client.
66
     */
67
    public function createResponse(ResponseData $data, QueryConfiguration $queryConfiguration, array $columnConfigurations)
68
    {
69
        $responseData = [
70
            'sEcho'                 => $queryConfiguration->drawCall(),
71
            'iTotalRecords'         => $data->totalDataCount(),
72
            'iTotalDisplayRecords'  => $data->data()->count(),
73
            'aaData'                => $data->data()->toArray()
74
        ];
75
        return new JsonResponse($responseData);
76
    }
77
78
    /**
79
     * @return string The name of the view that this version should use fot the table.
80
     */
81
    public function getTableView()
82
    {
83
        return "viewTableStuff";
84
    }
85
86
    /**
87
     * @return string The name of the view that this version should use for the script.
88
     */
89
    public function getScriptView()
90
    {
91
        return "scriptViewStuff";
92
    }
93
}