Completed
Pull Request — master (#1523)
by José
02:00
created

ActionSplitter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\Plan\Solver;
26
27
use Phinx\Db\Plan\AlterTable;
28
29
/**
30
 * A Plan takes an Intent and transforms int into a sequence of
31
 * instructions that can be correctly executed by an AdapterInterface.
32
 *
33
 * The main focus of Plan is to arrange the actions in the most efficient
34
 * way possible for the database.
35
 */
36
class ActionSplitter
37
{
38
    /**
39
     * The fully qualified class name of the Action class to match for conflicts
40
     * @var string
41
     */
42
    protected $conflictClass;
43
44
    /**
45
     * The fully qualified class name of the Action class to match for conflicts, which
46
     * is the dual of $conflictClass. For example `AddColumn` and `DropColumn` are duals.
47
     * @var string
48
     */
49
    protected $conflictClassDual;
50
51
    /**
52
     * A callback used to signal the actual presence of a conflict, that will be used to
53
     * partition the AlterTable into non-conflicting parts.
54
     *
55
     * The callback receives as first argument amn instance of $conflictClass and as second
56
     * argument an instance of $conflictClassDual
57
     *
58
     * @var callable
59
     */
60
    protected $conflictFilter;
61
62
    /**
63
     * Comstructor
64
     *
65
     * @param string $conflictClass The fully qualified class name of the Action class to match for conflicts
66
     * @param string $conflictClassDual The fully qualified class name of the Action class to match for conflicts,
67
     * which is the dual of $conflictClass. For example `AddColumn` and `DropColumn` are duals.
68
     * @param callable $conflictFilter The collection of actions to inspect
69
     */
70
    public function __construct($conflictClass, $conflictClassDual, callable $conflictFilter)
71
    {
72
        $this->conflictClass = $conflictClass;
73
        $this->conflictClassDual = $conflictClassDual;
74
        $this->conflictFilter = $conflictFilter;
75
    }
76
77
    /**
78
     * Returs a sequence of AlterTable instructions that are non conflicting
79
     * based on the constructor parameters.
80
     *
81
     * @param AlterTable $alter The collection of actions to inspect
82
     * @return AlterTable[] A list of AlterTable that can be executed without
83
     * this type of conflict
84
     */
85
    public function __invoke(AlterTable $alter)
86
    {
87
        $actions = collection($alter->getActions());
88
        $conflictActions = $actions
89
            ->filter(function ($action) {
90
                return $action instanceof $this->conflictClass;
91
            })
92
            ->toList();
93
94
        $originalAlter = new AlterTable($alter->getTable());
95
        $newAlter = new AlterTable($alter->getTable());
96
97
        $actions
98
            ->map(function ($action) use ($conflictActions) {
99
                if (!$action instanceof $this->conflictClassDual) {
100
                    return [$action, null];
101
                }
102
103
                $found = false;
104
                $matches = $this->conflictFilter;
105
                foreach ($conflictActions as $ca) {
106
                    if ($matches($ca, $action)) {
107
                        $found = true;
108
                        break;
109
                    }
110
                }
111
112
                if ($found) {
113
                    return [null, $action];
114
                }
115
116
                return [$action, null];
117
            })
118
            ->each(function ($pair) use ($originalAlter, $newAlter) {
119
                list($original, $new) = $pair;
120
                if ($original) {
121
                    $originalAlter->addAction($original);
122
                }
123
                if ($new) {
124
                    $newAlter->addAction($new);
125
                }
126
            });
127
128
        return [$originalAlter, $newAlter];
129
    }
130
}
131