ForeignKey   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 92
ccs 0
cts 48
cp 0
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 9 1
A createName() 0 7 2
A getDoctrineTable() 0 9 2
A create() 0 31 6
1
<?php
2
3
namespace CodexShaper\DBM\Database\Schema;
4
5
use Doctrine\DBAL\Schema\ForeignKeyConstraint as DoctrineForeignKey;
6
use Doctrine\DBAL\Schema\SchemaException;
7
8
class ForeignKey
9
{
10
    /**
11
     * Create foreign key.
12
     *
13
     * @param array $foreignKey
14
     *
15
     * @return \Doctrine\DBAL\Schema\ForeignKeyConstraint
16
     */
17
    public static function create($foreignKey)
18
    {
19
        // Set the local table
20
        $localTable = null;
21
        if (isset($foreignKey['localTable'])) {
22
            $localTable = static::getDoctrineTable($foreignKey['localTable']);
23
        }
24
25
        $localColumns = $foreignKey['localColumns'];
26
        $foreignTable = $foreignKey['foreignTable'];
27
        $foreignColumns = $foreignKey['foreignColumns'];
28
        $options = $foreignKey['options'] ?? [];
29
30
        // Set the name
31
        $name = isset($foreignKey['name']) ? trim($foreignKey['name']) : '';
32
        if (empty($name)) {
33
            $table = isset($localTable) ? $localTable->getName() : null;
34
            $name = static::createName($localColumns, 'foreign', $table);
35
        } else {
36
            // $name = Identifier::validate($name, 'Foreign Key');
37
        }
38
39
        $doctrineForeignKey = new DoctrineForeignKey(
40
            $localColumns, $foreignTable, $foreignColumns, $name, $options
41
        );
42
43
        if (isset($localTable)) {
44
            $doctrineForeignKey->setLocalTable($localTable);
45
        }
46
47
        return $doctrineForeignKey;
48
    }
49
50
    /**
51
     * Get foreign key name.
52
     *
53
     * @param array $columns
54
     * @param string $type
55
     * @param string|null $table
56
     *
57
     * @return string
58
     */
59
    public static function createName($columns, $type, $table = null)
60
    {
61
        $table = isset($table) ? trim($table).'_' : '';
62
        $type = trim($type);
63
        $name = strtolower($table.implode('_', $columns).'_'.$type);
64
65
        return str_replace(['-', '.'], '_', $name);
66
    }
67
68
    /**
69
     * Get doctrine table.
70
     *
71
     * @param string $table
72
     *
73
     * @return \Doctrine\DBAL\Schema\Table
74
     */
75
    public static function getDoctrineTable($table)
76
    {
77
        $table = trim($table);
78
79
        if (! static::tableExists($table)) {
0 ignored issues
show
Bug introduced by
The method tableExists() does not exist on CodexShaper\DBM\Database\Schema\ForeignKey. ( Ignorable by Annotation )

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

79
        if (! static::/** @scrutinizer ignore-call */ tableExists($table)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
            throw SchemaException::tableDoesNotExist($table);
81
        }
82
83
        return static::manager()->listTableDetails($table);
0 ignored issues
show
Bug introduced by
The method manager() does not exist on CodexShaper\DBM\Database\Schema\ForeignKey. ( Ignorable by Annotation )

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

83
        return static::/** @scrutinizer ignore-call */ manager()->listTableDetails($table);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
    }
85
86
    /**
87
     * Get all foreignkeys as an array.
88
     *
89
     * @return array
90
     */
91
    public static function toArray(DoctrineForeignKey $foreignKey)
92
    {
93
        return [
94
            'name' => $foreignKey->getName(),
95
            'localTable' => $foreignKey->getLocalTableName(),
96
            'localColumns' => $foreignKey->getLocalColumns(),
97
            'foreignTable' => $foreignKey->getForeignTableName(),
98
            'foreignColumns' => $foreignKey->getForeignColumns(),
99
            'options' => $foreignKey->getOptions(),
100
        ];
101
    }
102
}
103