Completed
Push — master ( 02bdee...b2f71d )
by Nate
02:08
created

IntegrationConnectionNameColumn   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 5
dl 0
loc 34
c 0
b 0
f 0
ccs 0
cts 21
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
tableName() 0 1 ?
A safeUp() 0 23 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-integration/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-integration/
7
 */
8
9
namespace flipbox\craft\integration\migrations;
10
11
use Craft;
12
use craft\db\Migration;
13
use craft\helpers\StringHelper;
14
use yii\db\Query;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 2.0.0
19
 */
20
abstract class IntegrationConnectionNameColumn extends Migration
21
{
22
    /**
23
     * @return string
24
     */
25
    abstract protected static function tableName(): string;
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function safeUp()
31
    {
32
        $this->addColumn(
33
            static::tableName(),
34
            'name',
35
            $this->string()->after('id')->notNull()
36
        );
37
38
        $records = (new Query())
39
            ->from(static::tableName())
40
            ->select(['id', 'handle'])
41
            ->all();
42
43
        foreach ($records as $record) {
44
            Craft::$app->getDb()->createCommand()
45
                ->update(
46
                    static::tableName(),
47
                    ['name' => StringHelper::titleize($record['handle'])],
48
                    ['id' => $record['id']]
49
                )
50
                ->execute();
51
        }
52
    }
53
}
54