Completed
Push — 1.10 ( 3bcec0...1a0641 )
by
unknown
08:47
created

OroCRMSalesBundle   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 52
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 10 10 1
B changeOnDeleteToCascade() 25 25 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace OroCRM\Bundle\SalesBundle\Migrations\Schema\v1_25_5;
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