Completed
Pull Request — master (#1339)
by José
05:14
created

AddForeignKey   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B build() 0 22 4
A getForeignKey() 0 4 1
1
<?php
2
/**
3
 * Phinx
4
 *
5
 * (The MIT license)
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated * documentation files (the "Software"), to
9
 * deal in the Software without restriction, including without limitation the
10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11
 * sell copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
 * IN THE SOFTWARE.
24
 */
25
namespace Phinx\Db\Action;
26
27
use InvalidArgumentException;
28
use Phinx\Db\Table\ForeignKey;
29
use Phinx\Db\Table\Table;
30
31
class AddForeignKey extends Action
32
{
33
34
    /**
35
     * The foreign key to add
36
     *
37
     * @var ForeignKey
38
     */
39
    protected $foreignKey;
40
41
    /**
42
     * Constructor
43
     *
44
     * @param Table $table The table to add the foreign key to
45
     * @param ForeignKey $fk The foreign key to add
46
     */
47
    public function __construct(Table $table, ForeignKey $fk)
48
    {
49
        parent::__construct($table);
50
        $this->foreignKey = $fk;
51
    }
52
53
    public static function build(Table $table, $columns, $referencedTable, $referencedColumns = ['id'], array $options = [], $name = null)
54
    {
55
        if (is_string($referencedColumns)) {
56
            $referencedColumns = [$referencedColumns]; // str to array
57
        }
58
59
        if (is_string($referencedTable)) {
60
            $referencedTable = new Table($referencedTable);
61
        }
62
63
        $fk = new ForeignKey();
64
        $fk->setReferencedTable($referencedTable)
65
           ->setColumns($columns)
66
           ->setReferencedColumns($referencedColumns)
67
           ->setOptions($options);
68
69
        if ($name !== null) {
70
            $fk->setConstraint($name);
71
        }
72
73
        return new static($table, $fk);
74
    }
75
76
    /**
77
     * Returns the foreign key to be added
78
     *
79
     * @return ForeignKey
80
     */
81
    public function getForeignKey()
82
    {
83
        return $this->foreignKey;
84
    }
85
}
86