SelectPayload   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 119
Duplicated Lines 16.81 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 2
dl 20
loc 119
ccs 42
cts 45
cp 0.9333
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C getColumns() 0 22 7
A getLimit() 10 10 3
A getOffset() 10 10 3
B getOrder() 0 18 5
A getWhere() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Minerva\FrontQL\Adapter\Select;
4
5
use Minerva\FrontQL\Adapter\Select\Basis\Exception\InvalidColumnNameException;
6
use Minerva\FrontQL\Adapter\Where\WhereAdapter;
7
8
/**
9
 * Class SelectPayload
10
 *
11
 * @author  Lucas A. de Araújo <[email protected]>
12
 * @package Minerva\FrontQL\Adapter\Select
13
 */
14
class SelectPayload
15
{
16
    /**
17
     * Payload recebido do front-end
18
     *
19
     * @var array
20
     */
21
    protected $payload;
22
23
    /**
24
     * Constrói o payload
25
     *
26
     * @param array $payload
27
     */
28 1
    public function __construct(array $payload)
29
    {
30 1
        $this->payload = $payload;
31 1
    }
32
33
    /**
34
     * Retorna as colunas filtradas no select
35
     * @return array
36
     * @throws InvalidColumnNameException
37
     */
38 1
    public function getColumns()
39
    {
40
        // Se nenhuma coluna foi definida todas serão retornadas
41 1
        if(!isset($this->payload['columns']) || !is_array($this->payload['columns']))
42 1
            return ['*'];
43
44 1
        $regex = '/^[a-zA-Z_][a-zA-Z0-9_]*$/';
45 1
        $columns = array();
46
47 1
        foreach ($this->payload['columns'] as $column){
48
            // Valida o nome da coluna
49 1
            if(!is_string($column) || !preg_match($regex, $column))
50 1
                throw new InvalidColumnNameException();
51
52 1
            $columns[] = $column;
53 1
        }
54
55 1
        if(count($columns) == 0)
56 1
            return ['*'];
57
58 1
        return $columns;
59
    }
60
61
    /**
62
     * Retorna o limite de linhas
63
     *
64
     * @return int|null
65
     */
66 1 View Code Duplication
    public function getLimit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68 1
        if(!isset($this->payload['limit']))
69 1
            return 500;
70
71 1
        if(!is_numeric($this->payload['limit']))
72 1
            return 500;
73
74 1
        return (int) $this->payload['limit'];
75
    }
76
    
77
    /**
78
     * Retorna o offset de linhas
79
     *
80
     * @return int|null
81
     */
82 1 View Code Duplication
    public function getOffset()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84 1
        if(!isset($this->payload['offset']))
85 1
            return 0;
86
87
        if(!is_numeric($this->payload['offset']))
88
            return 0;
89
90
        return (int) $this->payload['offset'];
91
    }
92
93
    /**
94
     * Retorna a ordem definida pelo usuário em string
95
     *
96
     * @return string
97
     */
98 1
    public function getOrder()
99
    {
100 1
        $allowed = ['ASC', 'DESC'];
101
102 1
        if(!isset($this->payload['order']) || !is_array($this->payload['order']))
103 1
            return null;
104
105 1
        $orders = array();
106
107 1
        foreach ($this->payload['order'] as $order)
108
        {
109 1
            $columns  = implode(', ', $order[0]);
110 1
            $command  = in_array($order[1], $allowed) ? $order[1] : 'ASC';
111 1
            $orders[] = "{$columns} {$command}";
112 1
        }
113
114 1
        return implode(', ', $orders);
115
    }
116
117
    /**
118
     * Retorna o where do payload de seleção
119
     *
120
     * @return array
121
     */
122 1
    public function getWhere()
123
    {
124 1
        if(!is_array($this->payload['where']))
125 1
            return [];
126
127 1
        $adapter = new WhereAdapter();
128 1
        $where = $adapter->fromArray($this->payload['where']);
129
130 1
        return $where;
131
    }
132
}
133