CreateTranslationsTable   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 57 4
A down() 0 4 1
1
<?php
2
3
use Illuminate\Database\Migrations\Migration;
4
use Illuminate\Database\Schema\Blueprint;
5
use Illuminate\Support\Facades\Schema;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Schema. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
class CreateTranslationsTable extends Migration
8
{
9
    /**
10
     * Run the migrations.
11
     *
12
     * @return void
13
     */
14
    public function up()
15
    {
16
        if (!Schema::hasTable('translations')) {
17
            Schema::create(
18
                'translations', function (Blueprint $table) {
19
                    $table->increments('id');
20
                    $table->string('locale', 10);
21
                    $table->string('namespace')->default('*');
22
                    $table->string('group');
23
                    $table->string('item');
24
                    $table->text('text');
25
                    $table->boolean('unstable')->default(false);
26
                    $table->boolean('locked')->default(false);
27
28
                    $table->unique(['locale', 'namespace', 'group', 'item']);
29
30
                    $table->timestamps();
31
                    $table->softDeletes();
32
33
                    // @todo Tava no CMS
34
                    // $table->increments('id');
35
                    // $table->integer('entity_id');
36
                    // $table->string('entity_type');
37
                    // $table->text('entity_data')->nullable();
38
                    // $table->nullableTimestamps();
39
                    // $table->string('language')->nullable();
40
                }
41
            );
42
        }
43
44
        try {
45
            if (!Schema::hasTable('model_translations')) {
46
                Schema::create(
47
                    'model_translations', function (Blueprint $table) {
48
                        $table->increments('id');
49
50
                        $table->string('entity_id');
51
                        $table->string('entity_type');
52
                        $table->string('entity_data')->nullable();
53
54
                        
55
                        $table->string('language_code');
56
                        $table->string('country_code')->nullable();
57
                    
58
59
                        $table->foreign('language_code')->references('code')->on('languages');
60
                        $table->foreign('country_code')->references('code')->on('countries');
61
62
                        $table->unique(['entity_id', 'entity_type', 'language_code', 'country_code'], 'translation_unique');
63
                        // $table->primary(['entity_id', 'entity_type', 'language_code', 'country_code']); @todo
64
65
                        $table->timestamps();
66
                    }
67
                );
68
            }
69
            //code...
70
        } catch (\Throwable $th) {
71
            //throw $th;
72
        }
73
    }
74
75
    /**
76
     * Reverse the migrations.
77
     *
78
     * @return void
79
     */
80
    public function down()
81
    {
82
        Schema::dropIfExists('model_translations');
83
        Schema::dropIfExists('translations');
84
    }
85
}
86