Passed
Push — master ( dd30a0...b64fc0 )
by Jonathan
17:19
created

RecordsExport::withDescendants()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
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
        // Model class
189
        $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...
190
191
        // Filter on the selected domain if needed
192
        $query = $modelClass::inDomain($this->domain, $this->addDescendants);
193
194
        // Add conditions if needed
195
        if (!empty($this->conditions)) {
196
            // Search by column
197
            foreach ($this->conditions as $fieldName => $searchValue) {
198
                // Get field by name and search by field column
199
                $field = $this->module->getField($fieldName);
200
                if (isset($searchValue) && !is_null($field)) {
201
                    $query = uitype($field->uitype_id)->addConditionToSearchQuery($query, $field, $searchValue);
0 ignored issues
show
Bug introduced by
The property uitype_id does not exist on Uccello\Core\Models\Field. Did you mean uitype?
Loading history...
202
                }
203
            }
204
        }
205
206
        // Add order if needed
207
        if (!empty($this->order)) {
208
            foreach($this->order as $fieldColumn => $order) {
209
                $query = $query->orderBy($fieldColumn, $order);
210
            }
211
        }
212
213
        return $query;
214
    }
215
216
    /**
217
     * Defined how to format data.
218
     *
219
     * @param mixed $record Uccello record model
220
     * @return array
221
     */
222
    public function map($record): array
223
    {
224
        $map = [ ];
225
226
        // Add id if needed
227
        if ($this->addId) {
228
            $map[ "id" ] = $record->{$record->getKeyName()};
229
        }
230
231
        foreach ($this->module->fields as $field) {
232
            // Ignore hidden columns if needed
233
            if (!empty($this->columns) && !in_array($field->name, $this->columns)) {
234
                continue;
235
            }
236
237
            $uitype = uitype($field->uitype_id);
238
            $map[ $field->name ] = $uitype->getFormattedValueToDisplay($field, $record);
239
        }
240
241
        // Add timestamps if needed
242
        if ($this->addTimestamps) {
243
            $map[ "created_at" ] = $record->created_at;
244
            $map[ "updated_at" ] = $record->updated_at;
245
        }
246
247
        return $map;
248
    }
249
250
    /**
251
     * Define what headers to add.
252
     *
253
     * @return array
254
     */
255
    public function headings(): array
256
    {
257
        $headings = [ ];
258
259
        // Add id column label if needed
260
        if ($this->addId) {
261
            $headings[ ] = uctrans('field.id', $this->module);
262
        }
263
264
        // All translated field label
265
        foreach ($this->module->fields as $field) {
266
            // Ignore hidden columns if needed
267
            if (!empty($this->columns) && !in_array($field->name, $this->columns)) {
268
                continue;
269
            }
270
271
            $headings[ $field->name ] = uctrans($field->label, $this->module);
272
        }
273
274
        // Add timestamps columns labels if needed
275
        if ($this->addTimestamps) {
276
            $headings[ "created_at" ] = uctrans('field.created_at', $this->module);
277
            $headings[ "updated_at" ] = uctrans('field.updated_at', $this->module);
278
        }
279
280
        return $headings;
281
    }
282
}
283