Import::batch()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Models;
4
5
use App\Models\Traits\BelongsToElementset;
6
use App\Models\Traits\BelongsToUser;
7
use App\Models\Traits\BelongsToVocabulary;
8
use Backpack\CRUD\CrudTrait;
9
use Culpa\Traits\Blameable;
10
use Culpa\Traits\CreatedBy;
11
use Illuminate\Database\Eloquent\Collection;
12
use Illuminate\Database\Eloquent\Model as Model;
13
use Illuminate\Database\Eloquent\Relations\BelongsTo;
14
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
15
use Illuminate\Database\Eloquent\Relations\HasMany;
16
use Illuminate\Database\Eloquent\SoftDeletes;
17
18
/**
19
 * App\Models\Import
20
 *
21
 * @property int $id
22
 * @property \Carbon\Carbon|null $created_at
23
 * @property \Carbon\Carbon|null $updated_at
24
 * @property \Carbon\Carbon|null $deleted_at
25
 * @property string $source_file_name
26
 * @property string|null $source
27
 * @property string $map stores the serialized column map array
28
 * @property int $user_id
29
 * @property string $file_name
30
 * @property string $file_type
31
 * @property array $preprocess stores the serialized results of the pre-import process
32
 * @property array $results stores the serialized results of the import
33
 * @property \Carbon\Carbon|null $imported_at
34
 * @property int $total_processed_count
35
 * @property int $error_count
36
 * @property int $success_count
37
 * @property int|null $added_count
38
 * @property int|null $updated_count
39
 * @property int|null $deleted_count
40
 * @property int $batch_id
41
 * @property int $vocabulary_id
42
 * @property int $schema_id
43
 * @property int|null $export_id
44
 * @property int|null $token
45
 * @property array $instructions
46
 * @property mixed|null $errors
47
 * @property-read \App\Models\Batch|null $batch
48
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ConceptAttributeHistory[] $concept_history
49
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ConceptAttribute[] $concept_statements
50
 * @property-read \App\Models\Access\User\User|null $creator
51
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ElementAttributeHistory[] $element_history
52
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ElementAttribute[] $element_statements
53
 * @property-read \App\Models\Elementset|null $elementset
54
 * @property-read \App\Models\Export|null $export
55
 * @property string $worksheet
56
 * @property-read \App\Models\Access\User\User|null $user
57
 * @property-read \App\Models\Vocabulary|null $vocabulary
58
 * @method static bool|null forceDelete()
59
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Import onlyTrashed()
60
 * @method static bool|null restore()
61
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereAddedCount($value)
62
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereBatchId($value)
63
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereCreatedAt($value)
64
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereDeletedAt($value)
65
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereDeletedCount($value)
66
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereErrorCount($value)
67
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereErrors($value)
68
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereExportId($value)
69
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereFileName($value)
70
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereFileType($value)
71
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereId($value)
72
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereImportedAt($value)
73
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereInstructions($value)
74
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereMap($value)
75
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import wherePreprocess($value)
76
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereResults($value)
77
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereSchemaId($value)
78
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereSource($value)
79
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereSourceFileName($value)
80
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereSuccessCount($value)
81
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereToken($value)
82
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereTotalProcessedCount($value)
83
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereUpdatedAt($value)
84
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereUpdatedCount($value)
85
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereUserId($value)
86
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Import whereVocabularyId($value)
87
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Import withTrashed()
88
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Import withoutTrashed()
89
 * @mixin \Eloquent
90
 */
91
class Import extends Model
92
{
93
    const TABLE      = 'reg_file_import_history';
94
    protected $table = self::TABLE;
95
    use SoftDeletes, Blameable, CreatedBy;
96
    use CrudTrait;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\CrudTrait requires some properties which are not provided by App\Models\Import: $Type, $fakeColumns
Loading history...
97
    use BelongsToVocabulary, BelongsToElementset, BelongsToUser;
98
    protected $blameable = [
99
        'created' => 'user_id',
100
    ];
101
    protected $dates = [
102
        'deleted_at',
103
        'imported_at',
104
    ];
105
    protected $guarded = ['id'];
106
    protected $casts   = [
107
        'id'                    => 'integer',
108
        'map'                   => 'string',
109
        'user_id'               => 'integer',
110
        'vocabulary_id'         => 'integer',
111
        'schema_id'             => 'integer',
112
        'file_name'             => 'string',
113
        'source_file_name'      => 'string',
114
        'file_type'             => 'string',
115
        'batch_id'              => 'integer',
116
        'results'               => 'array',
117
        'preprocess'            => 'array',
118
        'total_processed_count' => 'integer',
119
        'error_count'           => 'integer',
120
        'success_count'         => 'integer',
121
        'instructions'          => 'array',
122
    ];
123
    public static $rules = [
124
        'map'              => 'max:65535',
125
        'file_name'        => 'max:255',
126
        'source_file_name' => 'max:255',
127
        'file_type'        => 'max:30',
128
        'preprocess'       => 'max:65535',
129
        'results'          => 'max:65535',
130
    ];
131
    /*
132
    |--------------------------------------------------------------------------
133
    | FUNCTIONS
134
    |--------------------------------------------------------------------------
135
    */
136
137
    public function conceptResourceStatements(): ?Collection
138
    {
139
        return $this->hasMany(ConceptAttribute::class, 'last_import_id', 'id')
140
            ->with('profile_property')
141
            ->get()
142
            ->where('profile_property.is_object_prop', true);
143
    }
144
145
    public function elementResourceStatements(): ?Collection
146
    {
147
        return $this->hasMany(ElementAttribute::class, 'last_import_id', 'id')
148
            ->with('profile_property')
149
            ->get()
150
            ->where('profile_property.is_object_prop', true);
151
    }
152
153
    /*
154
    |--------------------------------------------------------------------------
155
    | RELATIONS
156
    |--------------------------------------------------------------------------
157
    */
158
159
    public function batch(): ?BelongsTo
160
    {
161
        return $this->belongsTo(Batch::class, 'batch_id', 'id');
162
    }
163
164
    public function export(): ?BelongsTo
165
    {
166
        return $this->belongsTo(Export::class, 'export_id', 'id');
167
    }
168
169
    public function concept_statements(): ?HasMany
170
    {
171
        return $this->hasMany(ConceptAttribute::class, 'last_import_id', 'id');
172
    }
173
174
    public function concept_history(): ?HasMany
175
    {
176
        return $this->hasMany(ConceptAttributeHistory::class, 'import_id', 'id');
177
    }
178
179
    public function element_statements(): ?HasMany
180
    {
181
        return $this->hasMany(ElementAttribute::class, 'last_import_id', 'id');
182
    }
183
184
    public function element_history(): ?HasMany
185
    {
186
        return $this->hasMany(ElementAttributeHistory::class, 'import_id', 'id');
187
    }
188
189
    public function vocabularies(): ?BelongsToMany
190
    {
191
        return $this->morphedByMany(Vocabulary::class, 'importable')->withTimestamps();
192
    }
193
194
    public function elementsets(): ?BelongsToMany
195
    {
196
        return $this->morphedByMany(Elementset::class, 'importable')->withTimestamps();
197
    }
198
199
    /*
200
    |--------------------------------------------------------------------------
201
    | SCOPES
202
    |--------------------------------------------------------------------------
203
    */
204
205
    /*
206
    |--------------------------------------------------------------------------
207
    | ACCESSORS
208
    |--------------------------------------------------------------------------
209
    */
210
211
    /**
212
     * @param string $value
213
     *
214
     * @return string
215
     */
216
    public function getWorksheetAttribute($value): string
217
    {
218
        return $this->attributes['source_file_name'];
219
    }
220
221
    public function getInstructionsAttribute($value)
222
    {
223
        return json_decode($value, true);
224
    }
225
226
    /*
227
    |--------------------------------------------------------------------------
228
    | MUTATORS
229
    |--------------------------------------------------------------------------
230
    */
231
232
    /**
233
     * @param string $value
234
     *
235
     * @return string
236
     */
237
    public function setWorksheetAttribute($value): string
238
    {
239
        return $this->attributes['source_file_name'] = $value;
240
    }
241
}
242