Passed
Push — master ( 0f9eb8...340202 )
by Jonathan
24:22
created

RecordsExport::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
c 2
b 0
f 0
dl 0
loc 17
rs 10
cc 1
nc 1
nop 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
     * Flag to set if we must export or not the records of the descendants domains.
48
     * Default: false
49
     *
50
     * @var boolean
51
     */
52
    protected $addDescendants = false;
53
54
    /**
55
     * Columns to export
56
     *
57
     * @var array
58
     */
59
    protected $columns;
60
61
    /**
62
     * Filter conditions
63
     *
64
     * @var array
65
     */
66
    protected $conditions;
67
68
    /**
69
     * Sort order
70
     *
71
     * @var array
72
     */
73
    protected $order;
74
75
    /**
76
     * Set the domain from which we want to retrieve records.
77
     *
78
     * @param \Uccello\Core\Models\Domain $domain
79
     * @return \Uccello\Core\Exports\RecordsExport
80
     */
81
    public function forDomain($domain)
82
    {
83
        if (is_string($domain)) {
0 ignored issues
show
introduced by
The condition is_string($domain) is always false.
Loading history...
84
            $module = ucdomain($domain);
0 ignored issues
show
Unused Code introduced by
The assignment to $module is dead and can be removed.
Loading history...
85
        }
86
87
        $this->domain = $domain;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Set the module from which we want to export records.
94
     *
95
     * @param \Uccello\Core\Models\Domain $domain
96
     * @return \Uccello\Core\Exports\RecordsExport
97
     */
98
    public function forModule($module)
99
    {
100
        if (is_string($module)) {
101
            $module = ucmodule($module);
102
        }
103
104
        $this->module = $module;
105
106
        return $this;
107
    }
108
109
    /**
110
     * Specify that we want to export record id
111
     *
112
     * @return \Uccello\Core\Exports\RecordsExport
113
     */
114
    public function withId()
115
    {
116
        $this->addId = true;
117
118
        return $this;
119
    }
120
121
    /**
122
     * Specify that we want to export timestamps columns (created_at, updated_at)
123
     *
124
     * @return \Uccello\Core\Exports\RecordsExport
125
     */
126
    public function withTimestamps()
127
    {
128
        $this->addTimestamps = true;
129
130
        return $this;
131
    }
132
133
    /**
134
     * Specify that we want to export records of the descendants domains
135
     *
136
     * @return \Uccello\Core\Exports\RecordsExport
137
     */
138
    public function withDescendants()
139
    {
140
        $this->addDescendants = true;
141
142
        return $this;
143
    }
144
145
    /**
146
     * Specify that we want to export only certain columns
147
     *
148
     * @return \Uccello\Core\Exports\RecordsExport
149
     */
150
    public function withColumns($columns)
151
    {
152
        $this->columns = $columns;
153
154
        return $this;
155
    }
156
157
    /**
158
     * Specify that we want to filter records with search conditions
159
     *
160
     * @return \Uccello\Core\Exports\RecordsExport
161
     */
162
    public function withConditions($conditions)
163
    {
164
        $this->conditions = $conditions;
165
166
        return $this;
167
    }
168
169
    /**
170
     * Specify that we want to order records
171
     *
172
     * @return \Uccello\Core\Exports\RecordsExport
173
     */
174
    public function withOrder($order)
175
    {
176
        $this->order = $order;
177
178
        return $this;
179
    }
180
181
    /**
182
     * Returns the query to use to retrieve records.
183
     *
184
     * @return \Illuminate\Database\Query\Builder
185
     */
186
    public function query()
187
    {
188
        // Filter
189
        $filter = [
190
            'columns' => $this->columns,
191
            'conditions' => $this->conditions,
192
            'order' => $this->order,
193
        ];
194
195
        // Model class
196
        $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...
197
198
        // Filter on the selected domain if needed
199
        $query = $modelClass::inDomain($this->domain, $this->addDescendants)
200
                            ->filterBy($filter);
201
202
        return $query;
203
    }
204
205
    /**
206
     * Defined how to format data.
207
     *
208
     * @param mixed $record Uccello record model
209
     * @return array
210
     */
211
    public function map($record): array
212
    {
213
        $map = [ ];
214
215
        // Add id if needed
216
        if ($this->addId) {
217
            $map[ "id" ] = $record->{$record->getKeyName()};
218
        }
219
220
        foreach ($this->module->fields as $field) {
221
            // Ignore hidden columns if needed
222
            if (!empty($this->columns) && !in_array($field->name, $this->columns)) {
223
                continue;
224
            }
225
226
            $uitype = uitype($field->uitype_id);
227
            $map[ $field->name ] = $uitype->getFormattedValueToDisplay($field, $record);
228
        }
229
230
        // Add timestamps if needed
231
        if ($this->addTimestamps) {
232
            $map[ "created_at" ] = $record->created_at;
233
            $map[ "updated_at" ] = $record->updated_at;
234
        }
235
236
        return $map;
237
    }
238
239
    /**
240
     * Define what headers to add.
241
     *
242
     * @return array
243
     */
244
    public function headings(): array
245
    {
246
        $headings = [ ];
247
248
        // Add id column label if needed
249
        if ($this->addId) {
250
            $headings[ ] = uctrans('field.id', $this->module);
251
        }
252
253
        // All translated field label
254
        foreach ($this->module->fields as $field) {
255
            // Ignore hidden columns if needed
256
            if (!empty($this->columns) && !in_array($field->name, $this->columns)) {
257
                continue;
258
            }
259
260
            $headings[ $field->name ] = uctrans($field->label, $this->module);
261
        }
262
263
        // Add timestamps columns labels if needed
264
        if ($this->addTimestamps) {
265
            $headings[ "created_at" ] = uctrans('field.created_at', $this->module);
266
            $headings[ "updated_at" ] = uctrans('field.updated_at', $this->module);
267
        }
268
269
        return $headings;
270
    }
271
}
272