Completed
Push — master ( 16f29f...af14db )
by Jonathan
11:39
created

RecordsExport::withOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Uccello\Core\Exports;
4
5
use Maatwebsite\Excel\Concerns\FromQuery;
0 ignored issues
show
Bug introduced by
The type Maatwebsite\Excel\Concerns\FromQuery was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Maatwebsite\Excel\Concerns\WithMapping;
0 ignored issues
show
Bug introduced by
The type Maatwebsite\Excel\Concerns\WithMapping was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Maatwebsite\Excel\Concerns\WithHeadings;
0 ignored issues
show
Bug introduced by
The type Maatwebsite\Excel\Concerns\WithHeadings was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
0 ignored issues
show
Bug introduced by
The type Maatwebsite\Excel\Concerns\ShouldAutoSize was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Maatwebsite\Excel\Concerns\Exportable;
0 ignored issues
show
Bug introduced by
The type Maatwebsite\Excel\Concerns\Exportable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Illuminate\Support\Facades\Schema;
11
12
class RecordsExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize
13
{
14
    use Exportable;
15
16
    /**
17
     * The module from which export records.
18
     *
19
     * @var \Uccello\Core\Models\Module
20
     */
21
    protected $module;
22
23
    /**
24
     * The domain from which retrieve records.
25
     *
26
     * @var \Uccello\Core\Models\Domain
27
     */
28
    protected $domain;
29
30
    /**
31
     * Flag to set if we must export or not the record id.
32
     * Default: false
33
     *
34
     * @var boolean
35
     */
36
    protected $addId = false;
37
38
    /**
39
     * Flag to set if we must export or not timestamps columns (created_at, updated_at).
40
     * Default: false
41
     *
42
     * @var boolean
43
     */
44
    protected $addTimestamps = false;
45
46
    /**
47
     * Columns to export
48
     *
49
     * @var array
50
     */
51
    protected $columns;
52
53
    /**
54
     * Filter conditions
55
     *
56
     * @var array
57
     */
58
    protected $conditions;
59
60
    /**
61
     * Sort order
62
     *
63
     * @var array
64
     */
65
    protected $order;
66
67
    /**
68
     * Set the domain from which we want to retrieve records.
69
     *
70
     * @param \Uccello\Core\Models\Domain $domain
71
     * @return \Uccello\Core\Exports\RecordsExport
72
     */
73
    public function forDomain($domain)
74
    {
75
        if (is_string($domain)) {
0 ignored issues
show
introduced by
The condition is_string($domain) is always false.
Loading history...
76
            $module = ucdomain($domain);
0 ignored issues
show
Unused Code introduced by
The assignment to $module is dead and can be removed.
Loading history...
Bug introduced by
The function ucdomain was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
            $module = /** @scrutinizer ignore-call */ ucdomain($domain);
Loading history...
77
        }
78
79
        $this->domain = $domain;
80
81
        return $this;
82
    }
83
84
    /**
85
     * Set the module from which we want to export records.
86
     *
87
     * @param \Uccello\Core\Models\Domain $domain
88
     * @return \Uccello\Core\Exports\RecordsExport
89
     */
90
    public function forModule($module)
91
    {
92
        if (is_string($module)) {
93
            $module = ucmodule($module);
94
        }
95
96
        $this->module = $module;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Specify that we want to export record id
103
     *
104
     * @return \Uccello\Core\Exports\RecordsExport
105
     */
106
    public function withId()
107
    {
108
        $this->addId = true;
109
110
        return $this;
111
    }
112
113
    /**
114
     * Specify that we want to export timestamps columns (created_at, updated_at)
115
     *
116
     * @return \Uccello\Core\Exports\RecordsExport
117
     */
118
    public function withTimestamps()
119
    {
120
        $this->addTimestamps = true;
121
122
        return $this;
123
    }
124
125
    /**
126
     * Specify that we want to export only certain columns
127
     *
128
     * @return \Uccello\Core\Exports\RecordsExport
129
     */
130
    public function withColumns($columns)
131
    {
132
        $this->columns = $columns;
133
134
        return $this;
135
    }
136
137
    /**
138
     * Specify that we want to filter records with search conditions
139
     *
140
     * @return \Uccello\Core\Exports\RecordsExport
141
     */
142
    public function withConditions($conditions)
143
    {
144
        $this->conditions = $conditions;
145
146
        return $this;
147
    }
148
149
    /**
150
     * Specify that we want to order records
151
     *
152
     * @return \Uccello\Core\Exports\RecordsExport
153
     */
154
    public function withOrder($order)
155
    {
156
        $this->order = $order;
157
158
        return $this;
159
    }
160
161
    /**
162
     * Returns the query to use to retrieve records.
163
     *
164
     * @return \Illuminate\Database\Query\Builder
165
     */
166
    public function query()
167
    {
168
        // Model class
169
        $modelClass = $this->module->model_class;
0 ignored issues
show
Bug introduced by
The property model_class does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
170
171
        // Default query
172
        $query = $modelClass::query();
173
174
        // Filter on the selected domain if needed
175
        if (!empty($this->domain) && Schema::hasColumn((new $modelClass)->getTable(), 'domain_id')) {
176
            $query = $query->where('domain_id', $this->domain->id);
177
        }
178
179
        // Add conditions if needed
180
        if (!empty($this->conditions)) {
181
            // Search by column
182
            foreach ($this->conditions as $fieldName => $searchValue) {
183
                // Get field by name and search by field column
184
                $field = $this->module->getField($fieldName);
185
                if (isset($searchValue) && !is_null($field)) {
186
                    $query = $field->uitype->addConditionToSearchQuery($query, $field, $searchValue);
187
                }
188
            }
189
        }
190
191
        // Add order if needed
192
        if (!empty($this->order)) {
193
            foreach($this->order as $fieldColumn => $order) {
194
                $query = $query->orderBy($fieldColumn, $order);
195
            }
196
        }
197
198
        return $query;
199
    }
200
201
    /**
202
     * Defined how to format data.
203
     *
204
     * @param mixed $record Uccello record model
205
     * @return array
206
     */
207
    public function map($record): array
208
    {
209
        $map = [ ];
210
211
        // Add id if needed
212
        if ($this->addId) {
213
            $map[ "id" ] = $record->{$record->getKeyName()};
214
        }
215
216
        foreach ($this->module->fields as $field) {
217
            // Ignore hidden columns if needed
218
            if (!empty($this->columns) && !in_array($field->name, $this->columns)) {
219
                continue;
220
            }
221
222
            $map[ $field->name ] = $field->uitype->getFormattedValueToDisplay($field, $record);
223
        }
224
225
        // Add timestamps if needed
226
        if ($this->addTimestamps) {
227
            $map[ "created_at" ] = $record->created_at;
228
            $map[ "updated_at" ] = $record->updated_at;
229
        }
230
231
        return $map;
232
    }
233
234
    /**
235
     * Define what headers to add.
236
     *
237
     * @return array
238
     */
239
    public function headings(): array
240
    {
241
        $headings = [ ];
242
243
        // Add id column label if needed
244
        if ($this->addId) {
245
            $headings[ ] = uctrans('field.id', $this->module);
246
        }
247
248
        // All translated field label
249
        foreach ($this->module->fields as $field) {
250
            // Ignore hidden columns if needed
251
            if (!empty($this->columns) && !in_array($field->name, $this->columns)) {
252
                continue;
253
            }
254
255
            $headings[ $field->name ] = uctrans($field->label, $this->module);
256
        }
257
258
        // Add timestamps columns labels if needed
259
        if ($this->addTimestamps) {
260
            $headings[ "created_at" ] = uctrans('field.created_at', $this->module);
261
            $headings[ "updated_at" ] = uctrans('field.updated_at', $this->module);
262
        }
263
264
        return $headings;
265
    }
266
}
267