Issues (35)

src/components/MultilanguageMigration.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace Itstructure\AdminModule\components;
4
5
use yii\helpers\ArrayHelper;
6
7
/**
8
 * Class MultilanguageMigration
9
 * Class to extend migration with multilanguage.
10
 *
11
 * @package Itstructure\AdminModule\components
12
 *
13
 * @author Andrey Girnik <[email protected]>
14
 */
15
class MultilanguageMigration extends \yii\db\Migration
16
{
17
    /**
18
     * Postfix for translate table name.
19
     */
20
    const TRANSLATE_TABLE_POSTFIX = 'language';
21
22
    /**
23
     * Table name to contain the project languages.
24
     */
25
    const LANGUAGE_TABLE_NAME = 'language';
26
27
    /**
28
     * Creates table with timestamp fields: created_at and updated_at.
29
     *
30
     * @param string $table - table name.
31
     * @param array $columns - array with names and types of columns.
32
     * @param string|null $options - additional SQL code.
33
     *
34
     * @return void
35
     */
36
    public function createTableWithTimestamps(string $table, array $columns, string $options = null): void
37
    {
38
        $columns = ArrayHelper::merge($columns, [
39
            'created_at' => $this->dateTime(),
0 ignored issues
show
The method dateTime() does not exist on Itstructure\AdminModule\...\MultilanguageMigration. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

39
            'created_at' => $this->/** @scrutinizer ignore-call */ dateTime(),
Loading history...
40
            'updated_at' => $this->dateTime(),
41
        ]);
42
        $this->createTable($table, $columns, $options);
43
    }
44
45
    /**
46
     * Creates two tables: main table and translate table.
47
     * For example:
48
     * catalog:
49
     *  - id
50
     *  - created_at
51
     *  - updated_at
52
     *
53
     * catalog_language:
54
     *  - catalog_id
55
     *  - language_id
56
     *  - title
57
     *  - text
58
     *
59
     * @param string $table - table name which needs to be translated.
60
     * @param array  $multiLanguageColumns - list of multilanguage fields.
61
     * @param array  $columns - list of simple fields.
62
     * @param string|null $options - additional SQL code.
63
     *
64
     * @return void
65
     */
66
    public function createMultiLanguageTable(
67
        string $table,
68
        array $multiLanguageColumns,
69
        array$columns = [],
70
        string $options = null
71
    ): void {
72
73
        $this->createTableWithTimestamps($table, ArrayHelper::merge([
74
                'id' => $this->primaryKey()
0 ignored issues
show
The method primaryKey() does not exist on Itstructure\AdminModule\...\MultilanguageMigration. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

74
                'id' => $this->/** @scrutinizer ignore-call */ primaryKey()
Loading history...
75
            ], $columns)
76
        );
77
78
        $keyToPrimaryTable = $this->getKeyToPrimaryTable($table);
79
        $keyToLanguageTable = self::getKeyToLanguageTable();
80
81
        $languageTableKeys = [
82
            $keyToPrimaryTable => $this->integer(),
0 ignored issues
show
The method integer() does not exist on Itstructure\AdminModule\...\MultilanguageMigration. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

82
            $keyToPrimaryTable => $this->/** @scrutinizer ignore-call */ integer(),
Loading history...
83
            $keyToLanguageTable => $this->integer(),
84
            "PRIMARY KEY($keyToPrimaryTable, $keyToLanguageTable)",
85
        ];
86
87
        $translateTableName = $this->getTranslateTableName($table);
88
89
        $this->createTableWithTimestamps(
90
            $translateTableName,
91
            ArrayHelper::merge($languageTableKeys, $multiLanguageColumns),
92
            $options
93
        );
94
95
        $this->addForeignKey(
96
            $this->createFkName($translateTableName, $keyToPrimaryTable),
97
            $translateTableName,
98
            $keyToPrimaryTable,
99
            $table,
100
            'id',
101
            'CASCADE',
102
            'CASCADE'
103
        );
104
105
        $this->addForeignKey(
106
            $this->createFkName($translateTableName, $keyToLanguageTable),
107
            $translateTableName,
108
            $keyToLanguageTable,
109
            self::LANGUAGE_TABLE_NAME,
110
            'id',
111
            'CASCADE',
112
            'CASCADE'
113
        );
114
    }
115
116
    /**
117
     * Drop main table with translate table.
118
     *
119
     * @param string $table - main table name.
120
     *
121
     * @return void
122
     */
123
    public function dropMultiLanguageTable(string $table): void
124
    {
125
        $translateTableName = $this->getTranslateTableName($table);
126
127
        $foreignKeyToPrimary = $this->createFkName($translateTableName, $this->getKeyToPrimaryTable($table));
128
        $foreignKeyToLanguage = $this->createFkName($translateTableName, self::getKeyToLanguageTable());
129
130
        $this->dropForeignKey($foreignKeyToPrimary, $translateTableName);
131
        $this->dropForeignKey($foreignKeyToLanguage, $translateTableName);
132
133
        $this->dropTable($translateTableName);
134
        $this->dropTable($table);
135
    }
136
137
    /**
138
     * Returns key name for link translate table with languages table.
139
     *
140
     * @return string
141
     */
142
    public static function getKeyToLanguageTable(): string
143
    {
144
        return self::LANGUAGE_TABLE_NAME . '_id';
145
    }
146
147
    /**
148
     * Returns foreign key to other table.
149
     *
150
     * @param string $table
151
     * @param string $column
152
     *
153
     * @return string
154
     */
155
    private function createFkName(string $table, string $column): string
156
    {
157
        return 'fk-' . $table . '-' . $column;
158
    }
159
160
    /**
161
     * Returns table name for translates.
162
     *
163
     * @param string $table - main table name.
164
     *
165
     * @return string
166
     */
167
    private function getTranslateTableName(string $table): string
168
    {
169
        return $table . '_' . self::TRANSLATE_TABLE_POSTFIX;
170
    }
171
172
    /**
173
     * Returns key name for link translate table with main table.
174
     *
175
     * @param string $table - main table name.
176
     *
177
     * @return string
178
     */
179
    private function getKeyToPrimaryTable(string $table): string
180
    {
181
        return $table . '_id';
182
    }
183
}
184