Completed
Push — master ( e10af0...723f44 )
by Marcus Vinícius
12s queued 10s
created

Negotiation::negotiate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace MarcusCampos\Dealer;
4
5
use MarcusCampos\Dealer\Parser;
6
use Illuminate\Database\Eloquent\Collection;
7
8
class Negotiation
9
{
10
11
    /**
12
     * @var Parser
13
     */
14
    private $parser;
15
16
    /**
17
     * @var array
18
     */
19
    private $stack;
20
21
    /**
22
     * @var \Illuminate\Database\Eloquent\Model
23
     */
24
    private $model;
25
26
    public function __construct()
27
    {
28
        $this->parser = new Parser();
29
    }
30
31
    /**
32
     * Negotiate
33
     *
34
     * @param string $query
35
     * @return Collection
36
     */
37
    public function negotiate(string $query)
38
    {
39
        $this->stack = $this->parser->parse($query);
40
41
        $namespace = config('dealer.models.namespace') ?? 'App\\';
42
        $this->model = app($namespace.$this->stack['model']);
43
44
        $this->model = $this->relations()
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->relations()->filt...pBy()->orderBy()->get() of type object<Illuminate\Database\Eloquent\Collection> is incompatible with the declared type object<Illuminate\Database\Eloquent\Model> of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
            ->filters()
46
            ->limit()
47
            ->groupBy()
48
            ->orderBy()
49
            ->get();
50
51
        return $this->only($this->model, $this->stack['fields']['only']);
52
    }
53
54
    /**
55
     * Return only selected elements
56
     *
57
     * @param Collection $collection
58
     * @param array $fields
59
     * @return Collection
60
     */
61
    private function only(Collection $collection, array $fields)
62
    {
63
        $fields = array_filter($fields);
64
        
65
        if(!$fields || $fields[0] == '*') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
66
            return $collection;
67
        }
68
69
        return $collection->map(function ($model) use($fields) {
70
            return $model->only($fields);
71
        });
72
    }
73
74
    /**
75
     * Get relations
76
     *
77
     * @return Negotiation
78
     */
79
    private function relations()
80
    {
81
        $relations = [];
82
83
        if (array_key_exists('extends', $this->stack['fields'])) {
84
            foreach ($this->stack['fields']['extends'] as $value) {
85
                $this->stack['fields']['only'][] = $value['name'];
86
87
                if (!$value['args'] || $value['args'][0] == '*') {
88
                    $relations[] = $value['name'];
89
                    continue;
90
                }
91
                $relations[] = $value['name'] . ':' . implode(',', $value['args']);
92
            }
93
        }
94
95
        $this->model = $this->model->with($relations);
96
97
        return $this;
98
    }
99
100
    /**
101
     * Set filters
102
     *
103
     * @return Negotiation
104
     */
105
    private function filters()
106
    {
107
        $relations = [];
0 ignored issues
show
Unused Code introduced by
$relations is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
108
109
        if (array_key_exists('filters', $this->stack)) {
110
            if (array_key_exists('extends', $this->stack['filters'])) {
111
                foreach ($this->stack['filters']['extends'] as $value) {
112
                    $methodName = $value['name'];
113
114
                    if (!$value['args']) {
115
                        $this->model = $this->model->$value['name']();
116
                        continue;
117
                    }
118
119
                    $this->model = $this->model->$methodName(...$value['args']);
120
                }
121
            }
122
        }
123
124
        return $this;
125
    }
126
127
    /**
128
     * Set the sort
129
     *
130
     * @return Negotiation
131
     */
132
    private function orderBy()
133
    {
134
        if (array_key_exists('orderBy', $this->stack)) {
135
            $this->model = $this->model->orderBy($this->stack['orderBy'][0],$this->stack['orderBy'][1]);
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * Set limit
143
     *
144
     * @return Negotiation
145
     */
146
    private function limit()
147
    {
148
        if (array_key_exists('limit', $this->stack)) {
149
            $this->model = $this->model->limit($this->stack['limit']);
150
        }
151
152
        return $this;
153
    }
154
155
    /**
156
     * Set groups
157
     *
158
     * @return Negotiation
159
     */
160 View Code Duplication
    private function groupBy()
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...
161
    {
162
        if (array_key_exists('groupBy', $this->stack)) {
163
            $this->model = $this->model->groupBy($this->stack['groupBy']);
164
        }
165
        return $this;
166
    }
167
168
    /**
169
     * Get data
170
     *
171
     * @return Collection
172
     */
173 View Code Duplication
    private function get()
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...
174
    {
175
        if (array_key_exists('paginate', $this->stack)) {
176
            return $this->model->paginate($this->stack['paginate']);
177
        }
178
179
        return $this->model->get();
180
    }
181
}