Passed
Branch beta (1b8e35)
by Jon
07:16
created

Element::SelectElementsByProject()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 28
nc 1
nop 1
1
<?php
2
3
namespace App\Models;
4
5
use App\Helpers\Macros\Traits\Languages;
6
use App\Models\Traits\BelongsToElementset;
7
use App\Models\Traits\HasStatus;
8
use Culpa\Traits\Blameable;
9
use Culpa\Traits\CreatedBy;
10
use Culpa\Traits\DeletedBy;
11
use Culpa\Traits\UpdatedBy;
12
use Illuminate\Database\Eloquent\Model;
13
use Illuminate\Database\Eloquent\Relations\HasMany;
14
use Illuminate\Database\Eloquent\SoftDeletes;
15
use Laracasts\Matryoshka\Cacheable;
16
17
/**
18
 * App\Models\Element.
19
 *
20
 * @property int $id
21
 * @property \Carbon\Carbon|null $created_at
22
 * @property \Carbon\Carbon|null $updated_at
23
 * @property \Carbon\Carbon|null $deleted_at
24
 * @property int|null $created_user_id
25
 * @property int|null $updated_user_id
26
 * @property int|null $deleted_user_id
27
 * @property int $schema_id
28
 * @property string|null $name
29
 * @property string|null $label
30
 * @property string|null $definition
31
 * @property string|null $comment
32
 * @property string $type
33
 * @property int|null $is_subproperty_of
34
 * @property string|null $parent_uri
35
 * @property string $uri
36
 * @property int $status_id
37
 * @property string $language
38
 * @property string|null $note
39
 * @property string|null $domain
40
 * @property string|null $orange
41
 * @property int|null $is_deprecated Boolean. Has this class/property been deprecated
42
 * @property string|null $url
43
 * @property string|null $lexical_alias
44
 * @property string $hash_id
45
 * @property int|null $created_by
46
 * @property int|null $updated_by
47
 * @property int|null $deleted_by
48
 * @property-read \App\Models\Access\User\User|null $creator
49
 * @property-read \App\Models\Elementset $elementset
50
 * @property-read \App\Models\Access\User\User|null $eraser
51
 * @property mixed $languages
52
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ElementAttribute[] $statements
53
 * @property-read \App\Models\Status $status
54
 * @property-read \App\Models\Access\User\User|null $updater
55
 * @method static bool|null forceDelete()
56
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Element onlyTrashed()
57
 * @method static bool|null restore()
58
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereComment($value)
59
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereCreatedAt($value)
60
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereCreatedBy($value)
61
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereCreatedUserId($value)
62
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereDefinition($value)
63
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereDeletedAt($value)
64
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereDeletedBy($value)
65
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereDeletedUserId($value)
66
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereDomain($value)
67
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereHashId($value)
68
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereId($value)
69
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereIsDeprecated($value)
70
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereIsSubpropertyOf($value)
71
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereLabel($value)
72
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereLanguage($value)
73
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereLexicalAlias($value)
74
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereName($value)
75
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereNote($value)
76
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereOrange($value)
77
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereParentUri($value)
78
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereSchemaId($value)
79
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereStatusId($value)
80
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereType($value)
81
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereUpdatedAt($value)
82
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereUpdatedBy($value)
83
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereUpdatedUserId($value)
84
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereUri($value)
85
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Element whereUrl($value)
86
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Element withTrashed()
87
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Element withoutTrashed()
88
 * @mixin \Eloquent
89
 */
90
class Element extends Model
91
{
92
    const TABLE           = 'reg_schema_property';
93
    const FORM_PROPERTIES = [1, 2, 3, 4, 5, 6, 7, 9, 11, 12, 13, 14, 27];
94
    protected $table      = self::TABLE;
95
    use SoftDeletes, Blameable, CreatedBy, UpdatedBy, DeletedBy;
96
    use Cacheable;
97
    use Languages, HasStatus, BelongsToElementset;
98
    protected $blameable = [
99
        'created' => 'created_user_id',
100
        'updated' => 'updated_user_id',
101
        'deleted' => 'deleted_user_id',
102
    ];
103
    protected $dates   = ['deleted_at'];
104
    protected $guarded = ['id'];
105
106
    /*
107
    |--------------------------------------------------------------------------
108
    | FUNCTIONS
109
    |--------------------------------------------------------------------------
110
    */
111
112
    /**
113
     * @param int $projectId
114
     *
115
     * @return array
116
     */
117
    public static function SelectElementsByProject($projectId)
118
    {
119
        return \DB::table(ElementAttribute::TABLE)
120
            ->join(self::TABLE,
121
                self::TABLE . '.id',
122
                '=',
123
                ElementAttribute::TABLE . '.schema_property_id')
124
            ->join(Elementset::TABLE,
125
                Elementset::TABLE . '.id',
126
                '=',
127
                self::TABLE . '.schema_id')
128
            ->select(ElementAttribute::TABLE . '.schema_property_id as id',
129
                Elementset::TABLE . '.name as Elementset',
130
                ElementAttribute::TABLE . '.language',
131
                ElementAttribute::TABLE . '.object as label')
132
            ->where([
133
                [ElementAttribute::TABLE . '.profile_property_id', 2],
134
                [Elementset::TABLE . '.agent_id', $projectId],
135
            ])
136
            ->orderBy(Elementset::TABLE . '.name')
137
            ->orderBy(ElementAttribute::TABLE . '.language')
138
            ->orderBy(ElementAttribute::TABLE . '.object')
139
            ->get()
140
            ->mapWithKeys(function ($item) {
141
                return [
142
                    $item->id . '_' . $item->language => $item->Elementset .
143
                        ' - (' .
144
                        $item->language .
145
                        ') ' .
146
                        $item->label,
147
                ];
148
            })
149
            ->toArray();
150
    }
151
152
    public function updateFromStatements(array $statements = null): self
153
    {
154
        $language    = $this->language;
155
        if (! $statements) {
156
            $s          =
157
                collect($this->statements->whereIn('profile_property_id', self::FORM_PROPERTIES)->toArray());
158
            $statements = $s->filter(function ($item) use ($language) {
159
                return $item['language'] === $language || $item['language'] === '';
160
            })->keyBy(function ($item) {
161
                return $item['profile_property_id'];
162
            })->map(function ($item) {
163
                return $item['object'];
164
            });
165
        }
166
        if (isset($statements['1'])) {
167
            $this->name = $statements['1'];
168
        }
169
        if (isset($statements['2'])) {
170
            $this->label = $statements['2'];
171
        }
172
        if (isset($statements['3'])) {
173
            $this->definition = $statements['3'];
174
        }
175
        if (isset($statements['4'])) {
176
            $this->type = $statements['4'];
177
        }
178
        if (isset($statements['5'])) {
179
            $this->comment = $statements['5'];
180
        }
181
        if (strtolower($this->type) === 'property') {
182
            if (isset($statements['6'])) {
183
                $this->parent_uri = $statements['6'];
184
            }
185
        } else {
186
            if (isset($statements['9'])) {
187
                $this->parent_uri = $statements['9'];
188
            }
189
        }
190
        if (isset($statements['7'])) {
191
            $this->note = $statements['7'];
192
        }
193
        if (isset($statements['11'])) {
194
            $this->domain = $statements['11'];
195
        }
196
        if (isset($statements['12'])) {
197
            $this->orange = $statements['12'];
198
        }
199
        if (isset($statements['13'])) {
200
            $this->uri = $statements['13'];
201
        }
202
        if (isset($statements['14'])) {
203
            $this->status_id =
204
                is_numeric($statements['14']) ? $statements['14'] :
205
                    Status::getByName($statements['14'])->id;
206
        }
207
        if (isset($statements['27'])) {
208
            $this->lexical_alias = $statements['27'];
209
        }
210
        //$this->url    = $statements["45"];
1 ignored issue
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
211
        $this->save();
212
213
        return $this;
214
    }
215
216
    /*
217
    |--------------------------------------------------------------------------
218
    | RELATIONS
219
    |--------------------------------------------------------------------------
220
    */
221
222
    public function statements(): ?HasMany
223
    {
224
        return $this->hasMany(ElementAttribute::class, 'schema_property_id', 'id');
225
    }
226
227
    /*
228
    |--------------------------------------------------------------------------
229
    | SCOPES
230
    |--------------------------------------------------------------------------
231
    */
232
233
    /*
234
    |--------------------------------------------------------------------------
235
    | ACCESSORS
236
    |--------------------------------------------------------------------------
237
    */
238
239
    /*
240
    |--------------------------------------------------------------------------
241
    | MUTATORS
242
    |--------------------------------------------------------------------------
243
    */
244
}
245