DataController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 11
dl 0
loc 81
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCondition() 0 13 4
C getAction() 0 51 8
1
<?php
2
/**
3
 * @company MTE Telecom, Ltd.
4
 * @author Roman Malashin <[email protected]>
5
 */
6
7
namespace Nnx\DataGrid\Controller;
8
9
use Nnx\DataGrid\Adapter\DoctrineDBAL;
10
use Nnx\DataGrid\Condition\Conditions;
11
use Zend\Mvc\Controller\AbstractActionController;
12
use Nnx\DataGrid\GridInterface;
13
use Nnx\DataGrid\Condition\SimpleCondition;
14
use Zend\View\Model\ViewModel;
15
16
/**
17
 * Class DataController
18
 * @package Nnx\DataGrid\Controller
19
 */
20
class DataController extends AbstractActionController
21
{
22
    /**
23
     * Возвращает данные для таблиц
24
     * @return ViewModel
25
     * @throws Exception\InvalidConditionNameException
26
     * @throws Exception\InvalidConditionValueException
27
     * @throws Exception\InvalidGridNameException
28
     */
29
    public function getAction()
30
    {
31
        /** @var \ZF\ContentNegotiation\Request $request */
32
        $request = $this->getRequest();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
33
        $gridName = $request->getQuery('grid');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
34
        $limit = (int)$request->getQuery('limit', 25);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
35
        $offset = (int)$request->getQuery('offset', 0);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
36
        $orderField = $request->getQuery('sidx', null);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
37
        $orderType = $request->getQuery('sorder', null);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
38
        $collapsedRows = is_array($request->getPost('collapsedRows')) ? $request->getPost('collapsedRows') : [];
39
        /**
40
         * $conditions => [
41
         *      [
42
         *          'key' => $name,
43
         *          'value' => $value
44
         *      ]
45
         * ];
46
         */
47
        $conditions = $request->getQuery('conditions', []);
48
        if (!$gridName) {
49
            throw new Exception\InvalidGridNameException('Не задано имя таблицы для получения данных');
50
        }
51
        /** @var GridInterface $grid */
52
        $grid = $this->getServiceLocator()->get('GridManager')->get('grids.' . $gridName);
53
54
        $adapter = $grid->getAdapter();
55
        if ($adapter instanceof DoctrineDBAL) {
56
            $adapter->setLimit($limit);
57
            $adapter->setOffset($offset);
58
            if ($orderField && $orderType) {
59
                $adapter->setOrder([
60
                    [
61
                        'field' => $orderField,
62
                        'order' => $orderType
63
                    ]
64
                ]);
65
            }
66
        }
67
68
        $conditionsObj = new Conditions();
69
        foreach ($conditions as $cond) {
0 ignored issues
show
Bug introduced by
The expression $conditions of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
70
            if (is_array($cond)) {
71
                $condition = $this->getCondition($cond);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
72
                $conditionsObj[] = $condition;
73
            }
74
        }
75
        $grid->setConditions($conditionsObj);
76
        $result = new ViewModel(['grid' => $grid, 'collapsedRows' => $collapsedRows]);
77
        $result->setTerminal(true);
78
        return $result;
79
    }
80
81
    /**
82
     * @param array $conditionData
83
     * @return SimpleCondition
84
     * @throws Exception\InvalidConditionNameException
85
     * @throws Exception\InvalidConditionValueException
86
     */
87
    protected function getCondition(array $conditionData)
88
    {
89
        if (!array_key_exists('key', $conditionData) || !$conditionData['key']) {
90
            throw new Exception\InvalidConditionNameException('Не задано имя условия для выборки');
91
        }
92
93
        if (!array_key_exists('value', $conditionData)) {
94
            throw new Exception\InvalidConditionValueException('Не передано значение для условия выборки.');
95
        }
96
97
        $conditionType = SimpleCondition::CRITERIA_TYPE_EQUAL;
98
        return new SimpleCondition($conditionData['key'], $conditionType, $conditionData['value']);
99
    }
100
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
101