Conditions | 15 |
Paths | 71 |
Total Lines | 107 |
Code Lines | 76 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
29 | public function safeUp() |
||
30 | { |
||
31 | |||
32 | // Don't make the same config changes twice |
||
33 | $schemaVersion = Craft::$app->projectConfig |
||
34 | ->get('plugins.cache-flag.schemaVersion', true); |
||
35 | |||
36 | if (\version_compare($schemaVersion, '1.0.1', '>=')) { |
||
37 | return; |
||
38 | } |
||
39 | |||
40 | if (!!Flags::find()->count()) { |
||
41 | // There's already content in the Craft 3 table, so don't attempt to migrate anything |
||
42 | return; |
||
43 | } |
||
44 | |||
45 | $craft2TableSchema = Craft::$app->db->schema->getTableSchema('{{%templatecaches_flags}}'); |
||
46 | if ($craft2TableSchema === null) { |
||
47 | return; |
||
48 | } |
||
49 | |||
50 | // Get all migratable flags |
||
51 | $baseQuery = (new Query()) |
||
52 | ->select(['flags', 'sectionId', 'categoryGroupId', 'tagGroupId', 'userGroupId', 'assetSourceId', 'globalSetId', 'elementType', 'flags.dateCreated', 'flags.dateUpdated', 'flags.uid']) |
||
53 | ->from('{{%templatecaches_flags}} AS flags'); |
||
54 | |||
55 | $rowsToInsert = []; |
||
56 | |||
57 | $rowsToInsert += (clone $baseQuery) |
||
58 | ->innerJoin('{{%sections}} AS sections', 'sections.id=sectionId') |
||
59 | ->all(); |
||
60 | |||
61 | $rowsToInsert += (clone $baseQuery) |
||
62 | ->innerJoin('{{%categorygroups}} AS categorygroups', 'categorygroups.id=categoryGroupId') |
||
63 | ->all(); |
||
64 | |||
65 | $rowsToInsert += (clone $baseQuery) |
||
66 | ->innerJoin('{{%taggroups}} AS taggroups', 'taggroups.id=tagGroupId') |
||
67 | ->all(); |
||
68 | |||
69 | $rowsToInsert += (clone $baseQuery) |
||
70 | ->innerJoin('{{%usergroups}} AS usergroups', 'usergroups.id=userGroupId') |
||
71 | ->all(); |
||
72 | |||
73 | $volumeRows = (clone $baseQuery) |
||
74 | ->innerJoin('{{%volumes}} AS volumes', 'volumes.id=assetSourceId') |
||
75 | ->all(); |
||
76 | |||
77 | foreach ($volumeRows as $volumeRow) { |
||
78 | $volumeRow['volumeId'] = $volumeRow['assetSourceId']; |
||
79 | unset($volumeRow['assetSourceId']); |
||
80 | $rowsToInsert[] = $volumeRow; |
||
81 | } |
||
82 | |||
83 | $rowsToInsert += (clone $baseQuery) |
||
84 | ->innerJoin('{{%globalsets}} AS globalsets', 'globalsets.id=globalSetId') |
||
85 | ->all(); |
||
86 | |||
87 | $elementTypeRows = (clone $baseQuery) |
||
88 | ->where('elementType IS NOT NULL') |
||
89 | ->all(); |
||
90 | foreach ($elementTypeRows as $elementTypeRow) { |
||
91 | $elementType = $elementTypeRow['elementType']; |
||
92 | switch ($elementType) { |
||
93 | case 'Entry': |
||
94 | $elementType = Entry::class; |
||
95 | break; |
||
96 | case 'Category': |
||
97 | $elementType = Category::class; |
||
98 | break; |
||
99 | case 'Tag': |
||
100 | $elementType = Tag::class; |
||
101 | break; |
||
102 | case 'User': |
||
103 | $elementType = User::class; |
||
104 | break; |
||
105 | case 'Asset': |
||
106 | $elementType = Asset::class; |
||
107 | break; |
||
108 | case 'GlobalSet': |
||
109 | $elementType = GlobalSet::class; |
||
110 | break; |
||
111 | case 'MatrixBlock': |
||
112 | $elementType = MatrixBlock::class; |
||
113 | break; |
||
114 | default: |
||
115 | $elementType = null; |
||
116 | } |
||
117 | if (!$elementType) { |
||
118 | continue; |
||
119 | } |
||
120 | $elementTypeRow['elementType'] = $elementType; |
||
121 | $rowsToInsert[] = $elementTypeRow; |
||
122 | } |
||
123 | |||
124 | if (!empty($rowsToInsert)) { |
||
125 | $this |
||
126 | ->batchInsert( |
||
127 | '{{%cacheflag_flags}}', |
||
128 | ['flags', 'sectionId', 'categoryGroupId', 'tagGroupId', 'userGroupId', 'volumeId', 'globalSetId', 'elementType', 'dateCreated', 'dateUpdated', 'uid'], |
||
129 | $rowsToInsert, |
||
130 | false |
||
131 | ); |
||
132 | } |
||
133 | |||
134 | // Delete the Craft 2 table |
||
135 | $this->dropTableIfExists('{{%templatecaches_flags}}'); |
||
136 | } |
||
148 |