Export::setSelectedColumnsAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

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 2
eloc 1
nc 2
nop 1
1
<?php
2
3
/**
4
 * Created by Reliese Model.
5
 * Date: Tue, 09 May 2017 17:01:29 +0000.
6
 */
7
8
namespace App\Models;
9
10
use App\Models\Traits\BelongsToElementset;
11
use App\Models\Traits\BelongsToProfile;
12
use App\Models\Traits\BelongsToVocabulary;
13
use Backpack\CRUD\CrudTrait;
14
use Culpa\Traits\Blameable;
15
use Culpa\Traits\CreatedBy;
16
use Illuminate\Database\Eloquent\Model;
17
use Illuminate\Database\Eloquent\Relations\HasMany;
18
use const true;
19
use function is_iterable;
20
use function unserialize;
21
22
/**
23
 * App\Models\Export
24
 *
25
 * @property int $id
26
 * @property \Carbon\Carbon|null $created_at
27
 * @property \Carbon\Carbon|null $updated_at
28
 * @property int $user_id
29
 * @property int $vocabulary_id
30
 * @property int $schema_id
31
 * @property bool $exclude_deprecated
32
 * @property bool $include_generated
33
 * @property bool $include_deleted
34
 * @property bool $include_not_accepted
35
 * @property \Illuminate\Support\Collection|null $selected_columns
36
 * @property string|null $selected_language
37
 * @property string|null $published_english_version
38
 * @property string|null $published_language_version
39
 * @property \Carbon\Carbon|null $last_vocab_update
40
 * @property int $profile_id
41
 * @property string|null $file
42
 * @property \Illuminate\Support\Collection|null $map
43
 * @property-read \App\Models\Access\User\User|null $creator
44
 * @property-read \App\Models\Elementset|null $elementset
45
 * @property-read mixed $languages
46
 * @property-read mixed $worksheet
47
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Import[] $imports
48
 * @property-read \App\Models\Profile|null $profile
49
 * @property-read \App\Models\Vocabulary|null $vocabulary
50
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereCreatedAt($value)
51
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereExcludeDeprecated($value)
52
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereFile($value)
53
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereId($value)
54
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereIncludeDeleted($value)
55
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereIncludeGenerated($value)
56
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereIncludeNotAccepted($value)
57
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereLastVocabUpdate($value)
58
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereMap($value)
59
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereProfileId($value)
60
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export wherePublishedEnglishVersion($value)
61
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export wherePublishedLanguageVersion($value)
62
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereSchemaId($value)
63
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereSelectedColumns($value)
64
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereSelectedLanguage($value)
65
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereUpdatedAt($value)
66
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereUserId($value)
67
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereVocabularyId($value)
68
 * @mixin \Eloquent
69
 */
70
class Export extends Model
71
{
72
    const TABLE   = 'reg_export_history';
73
    public $table = self::TABLE;
74
    use CrudTrait;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\CrudTrait requires some properties which are not provided by App\Models\Export: $Type, $fakeColumns
Loading history...
75
    use Blameable, CreatedBy;
76
    use BelongsToProfile, BelongsToVocabulary, BelongsToElementset;
77
    protected $blameable = [
78
        'created' => 'user_id',
79
    ];
80
    protected $dates = [
81
        'last_vocab_update',
82
    ];
83
    protected $guarded = ['id'];
84
    protected $casts   = [
85
        'user_id'              => 'int',
86
        'vocabulary_id'        => 'int',
87
        'schema_id'            => 'int',
88
        'exclude_deprecated'   => 'bool',
89
        'include_generated'    => 'bool',
90
        'include_deleted'      => 'bool',
91
        'include_not_accepted' => 'bool',
92
        'profile_id'           => 'int',
93
    ];
94
    public static $rules = [];
95
96
    /*
97
    |--------------------------------------------------------------------------
98
    | FUNCTIONS
99
    |--------------------------------------------------------------------------
100
    */
101
102
    public static function findByExportFileName(string $name, $extension = '.csv'): ?self
103
    {
104
        //strip the csv before searching
105
        if (ends_with($name, $extension)) {
106
            $name = str_before($name, $extension);
107
        }
108
109
        return self::whereFile($name . $extension)->first();
110
    }
111
112
    /**
113
     * @param Import|iterable $imports
114
     *
115
     * @return $this
116
     */
117
    public function addImports($imports)
118
    {
119
        if (is_iterable($imports)) {
120
            $this->imports()->saveMany($imports);
0 ignored issues
show
Bug introduced by
It seems like $imports can also be of type App\Models\Import; however, parameter $models of Illuminate\Database\Eloq...asOneOrMany::saveMany() does only seem to accept Traversable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

120
            $this->imports()->saveMany(/** @scrutinizer ignore-type */ $imports);
Loading history...
121
        } else {
122
            $this->imports()->save($imports);
123
        }
124
125
        return $this;
126
    }
127
128
    /**
129
     * @return Import|null
130
     */
131
    public function getLatestImport(): ?Import
132
    {
133
        return $this->imports()->latest()->first();
134
    }
135
136
    /*
137
    |--------------------------------------------------------------------------
138
    | RELATIONS
139
    |--------------------------------------------------------------------------
140
    */
141
142
    public function imports(): ?HasMany
143
    {
144
        return $this->hasMany(Import::class);
145
    }
146
147
    /*
148
    |--------------------------------------------------------------------------
149
    | SCOPES
150
    |--------------------------------------------------------------------------
151
    */
152
153
    /*
154
    |--------------------------------------------------------------------------
155
    | ACCESSORS
156
    |--------------------------------------------------------------------------
157
    */
158
159
    /**
160
     * @return \Illuminate\Support\Collection|null
161
     */
162
    public function getMapAttribute($value)
163
    {
164
        return $value ? collect(unserialize($value, [true])) : null;
165
    }
166
167
    public function getWorksheetAttribute(): ?string
168
    {
169
        $arr = explode('_', $this->attributes['file']);
170
171
        return $arr[0] ?? null;
172
    }
173
174
    public function getLanguagesAttribute(): ?string
175
    {
176
        $arr = explode('_', $this->attributes['file']);
177
178
        return $arr[1] ?? null;
179
    }
180
181
    /**
182
     * @return \Illuminate\Support\Collection|null
183
     */
184
    public function getSelectedColumnsAttribute($value)
185
    {
186
        return $value ? collect(unserialize($value, [true])) : null;
187
    }
188
189
    /*
190
    |--------------------------------------------------------------------------
191
    | MUTATORS
192
    |--------------------------------------------------------------------------
193
    */
194
195
    /**
196
     * @param $value
197
     */
198
    public function setMapAttribute($value): void
199
    {
200
        $value ? $this->attributes['map'] = serialize($value) : null;
201
    }
202
203
    /**
204
     * @param $value
205
     */
206
    public function setSelectedColumnsAttribute($value): void
207
    {
208
        $value ? $this->attributes['selected_columns'] = serialize($value) : null;
209
    }
210
}
211