Completed
Push — 1.8 ( 8d6730...a2d9e7 )
by
unknown
42:28
created

OroCRMSalesBundle::changeOnDeleteToCascade()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
dl 25
loc 25
rs 8.439
c 0
b 0
f 0
cc 6
eloc 16
nc 6
nop 2
1
<?php
2
3
namespace OroCRM\Bundle\SalesBundle\Migrations\Schema\v1_19_1;
4
5
use Doctrine\DBAL\Schema\Schema;
6
use Oro\Bundle\MigrationBundle\Migration\Migration;
7
use Oro\Bundle\MigrationBundle\Migration\QueryBag;
8
9 View Code Duplication
class OroCRMSalesBundle implements Migration
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
10
{
11
    /**
12
     * Changes account_id to onDelete=CASCADE
13
     *
14
     * {@inheritdoc}
15
     */
16
    public function up(Schema $schema, QueryBag $queries)
17
    {
18
        $this->changeOnDeleteToCascade(
19
            $schema,
20
            [
21
                'orocrm_sales_b2bcustomer' => ['account_id'],
22
                'orocrm_sales_opportunity' => ['customer_id'],
23
            ]
24
        );
25
    }
26
27
    /**
28
     * @param Schema $schema
29
     * @param array $data
30
     * [
31
     *     table name => [column name, ...],
32
     *     ...
33
     * ]
34
     */
35
    protected function changeOnDeleteToCascade(Schema $schema, array $data)
36
    {
37
        foreach ($data as $tableName => $columns) {
38
            $table = $schema->getTable($tableName);
39
            foreach ($columns as $column) {
40
                $foreignKeys = $table->getForeignKeys();
41
                foreach ($foreignKeys as $foreignKey) {
42
                    $foreignKeyColumns = $foreignKey->getUnquotedLocalColumns();
43
                    if ($foreignKeyColumns === [$column]) {
44
                        if ($foreignKey->getOption('onDelete') !== 'CASCADE') {
45
                            $table->removeForeignKey($foreignKey->getName());
46
                            $table->addForeignKeyConstraint(
47
                                $foreignKey->getUnqualifiedForeignTableName(),
48
                                $foreignKeyColumns,
49
                                $foreignKey->getUnquotedForeignColumns(),
50
                                ['onDelete' => 'CASCADE', 'onUpdate' => $foreignKey->getOption('onUpdate')]
51
                            );
52
                        }
53
54
                        break;
55
                    }
56
                }
57
            }
58
        }
59
    }
60
}
61