Completed
Push — feature/backpack ( 16fb41...46736d )
by Chris
10:31 queued 01:05
created

AddTranslatedNameAndDescriptionToSkillsTable   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 18
dl 0
loc 40
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A down() 0 4 1
A up() 0 21 4
1
<?php
2
3
use Illuminate\Support\Facades\Schema;
4
use Illuminate\Support\Facades\Lang;
5
use Illuminate\Database\Schema\Blueprint;
6
use Illuminate\Database\Migrations\Migration;
7
8
use App\Models\Skill;
9
10
class AddTranslatedNameAndDescriptionToSkillsTable extends Migration
11
{
12
    /**
13
     * Run the migrations.
14
     *
15
     * @return void
16
     */
17
    public function up()
18
    {
19
        Schema::table('skills', function (Blueprint $table) {
20
            $table->json('name')->nullable();
21
            $table->json('description')->nullable();
22
        });
23
24
        $skills = Skill::all();
25
        $langEn = Lang::get('common/skills.skills', [], 'en');
26
        $langFr = Lang::get('common/skills.skills', [], 'fr');
27
28
        foreach ($skills as $skill) {
29
            if (!array_key_exists($skill->key, $langEn) || !array_key_exists($skill->key, $langFr)) {
0 ignored issues
show
Bug introduced by
The property key does not seem to exist on App\Models\Skill. 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...
Bug introduced by
It seems like $langEn can also be of type null and string; however, parameter $search of array_key_exists() does only seem to accept 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

29
            if (!array_key_exists($skill->key, /** @scrutinizer ignore-type */ $langEn) || !array_key_exists($skill->key, $langFr)) {
Loading history...
30
                continue;
31
            }
32
            $skill
33
                ->setTranslation('name', 'en', $langEn[$skill->key]['name'])
34
                ->setTranslation('name', 'fr', $langFr[$skill->key]['name'])
35
                ->setTranslation('description', 'en', $langEn[$skill->key]['description'])
36
                ->setTranslation('description', 'fr', $langFr[$skill->key]['description'])
37
                ->save();
38
        }
39
    }
40
41
    /**
42
     * Reverse the migrations.
43
     *
44
     * @return void
45
     */
46
    public function down()
47
    {
48
        Schema::table('skills', function (Blueprint $table) {
49
            $table->dropColumn(['name', 'description']);
50
        });
51
    }
52
}
53