Completed
Branch feature/pre-split (1fb89d)
by Anton
03:12
created

ReferenceSchema::sqlStatement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Database\Drivers\SQLite\Schemas;
10
11
use Spiral\Database\Entities\Driver;
12
use Spiral\Database\Schemas\Prototypes\AbstractReference;
13
14
class ReferenceSchema extends AbstractReference
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function sqlStatement(Driver $driver): string
20
    {
21
        $statement = [];
22
23
        $statement[] = 'FOREIGN KEY';
24
        $statement[] = '(' . $driver->identifier($this->column) . ')';
25
26
        $statement[] = 'REFERENCES ' . $driver->identifier($this->foreignTable);
27
        $statement[] = '(' . $driver->identifier($this->foreignKey) . ')';
28
29
        $statement[] = "ON DELETE {$this->deleteRule}";
30
        $statement[] = "ON UPDATE {$this->updateRule}";
31
32
        return implode(' ', $statement);
33
    }
34
35
    /**
36
     * @param string $table
37
     * @param array  $schema
38
     *
39
     * @return ReferenceSchema
40
     */
41 View Code Duplication
    public static function createInstance(string $table, array $schema): self
0 ignored issues
show
Duplication introduced by
This method 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...
42
    {
43
        $reference = new self($table, $schema['id']);
44
45
        $reference->column = $schema['from'];
46
47
        $reference->foreignTable = $schema['table'];
48
        $reference->foreignKey = $schema['to'];
49
50
        $reference->deleteRule = $schema['on_delete'];
51
        $reference->updateRule = $schema['on_update'];
52
53
        return $reference;
54
    }
55
}
56