Completed
Push — dev ( 6293b4...41c0d5 )
by James Ekow Abaka
03:00
created

Migrations::getMigrationPathsInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 13
ccs 0
cts 7
cp 0
crap 2
rs 9.8333
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Migrations::getAllPaths() 0 11 1
1
<?php
2
3
namespace yentu;
4
5
use clearice\io\Io;
6
use yentu\factories\DatabaseManipulatorFactory;
7
8
9
class Migrations
10
{
11
    private $manipulatorFactory;
12
    private $config;
13
    private $io;
14
15 41
    public function __construct(Io $io, DatabaseManipulatorFactory $manipulatorFactory, array $config)
16
    {
17 41
        $this->manipulatorFactory = $manipulatorFactory;
18 41
        $this->config = $config;
19 41
        $this->io = $io;
20 41
    }
21
22 9
    public function getAllPaths()
23
    {
24 9
        return array_merge(
25
            array(
26
                array(
27 9
                    'home' => $this->getPath('migrations'),
28 9
                    'variables' => $this->config['variables']
29
                )
30 9
            ), $this->config['other_migrations']
31
        );
32
    }
33
34
    /**
35
     * Return an array of all migrations available.
36
     *
37
     * @param string $path
38
     * @return array
39
     */
40 9
    public function getMigrationFiles($path)
41
    {
42 9
        if (!file_exists($path))
43
            return [];
44 9
        $migrationFiles = scandir($path, 0);
45 9
        $migrations = array();
46 9
        foreach ($migrationFiles as $migration) {
47 9
            $details = $this->getMigrationDetails($migration);
48 9
            if ($details === false)
49 9
                continue;
50 9
            $migrations[$details['timestamp']] = $details;
51 9
            unset($migrations[$details['timestamp']][0]);
52 9
            unset($migrations[$details['timestamp']][1]);
53 9
            unset($migrations[$details['timestamp']][2]);
54
        }
55
56 9
        return $migrations;
57
    }
58
59
    /**
60
     * Return the details of a migration extracted from the file name.
61
     * This method uses a regular expression to extract the timestamp and
62
     * migration name from the migration script.
63
     *
64
     * @param string $migration
65
     * @return array|bool
66
     */
67 9
    private function getMigrationDetails($migration)
68
    {
69 9
        if (preg_match("/^(?<timestamp>[0-9]{14})\_(?<migration>[a-z][a-z0-9\_]*)\.php$/", $migration, $details)) {
70 9
            $details['file'] = $migration;
71
        } else {
72 9
            $details = false;
73
        }
74 9
        return $details;
75
    }
76
    /**
77
     * Returns an array of all migrations, in all configured migrations
78
     * directories.
79
     * @return array
80
     */
81
    public function getAllMigrations()
82
    {
83
        $migrations = array();
84
        foreach ($this->getAllPaths() as $migration) {
85
            $migrations = $migrations + $this->getMigrationFiles($migration['home']);
86
        }
87
        return $migrations;
88
    }
89
90
91
    /**
92
     * Returns an array of all migrations that have been run on the database.
93
     * The information returned includes the timestamp, the name of the migration
94
     * and the default schema on which it was run.
95
     * @return array
96
     * @throws exceptions\DatabaseManipulatorException
97
     */
98
    public function getRunMirations()
99
    {
100
        $db = $this->manipulatorFactory->createManipulator();
101
        $runMigrations = $db->query("SELECT DISTINCT version, migration, default_schema FROM yentu_history ORDER BY version");
102
        $migrations = array();
103
        foreach ($runMigrations as $migration) {
104
            $migrations[$migration['version']] = array(
105
                'timestamp' => $migration['version'],
106
                'migration' => $migration['migration'],
107
                'default_schema' => $migration['default_schema']
108
            );
109
        }
110
111
        return $migrations;
112
    }
113
114
    /**
115
     * Returns a path relative to the current yentu home.
116
     * @param string $path
117
     * @return string
118
     */
119 41
    public function getPath($path)
120
    {
121 41
        return $this->config['home'] . DIRECTORY_SEPARATOR . $path;
122
    }
123
124
125
    /**
126
     * Announce a migration based on the command and the arguments called for
127
     * the migration.
128
     *
129
     * @param string $command The action being performed
130
     * @param string $itemType The type of item
131
     * @param array $arguments The arguments of the
132
     */
133 9
    public function announce($command, $itemType, $arguments)
134
    {
135 9
        $this->io->output(
136 9
            "\n  - " . ucfirst("{$command}ing ") .
137 9
            preg_replace("/([a-z])([A-Z])/", "$1 $2", $itemType) . " " .
138 9
            $this->getMigrationEventDescription($command, Parameters::wrap($arguments)), Io::OUTPUT_LEVEL_2
139
        );
140 9
        $this->io->output(".");
141 9
    }
142
143
    /**
144
     * Convert the arguments of a migration event to a string description.
145
     *
146
     * @param string $command
147
     * @param array $arguments
148
     * @return string
149
     */
150 9
    private function getMigrationEventDescription($command, $arguments)
151
    {
152 9
        $dir = '';
153 9
        $destination = '';
154 9
        $arguments = Parameters::wrap($arguments, ['name' => null]);
155
156 9
        if ($command == 'add') {
157 9
            $dir = 'to';
158 5
        } else if ($command == 'drop') {
159
            $dir = 'from';
160
        }
161
162 9
        if (isset($arguments['table']) && isset($arguments['schema'])) {
163
            $destination = "table " .
164 9
                ($arguments['schema'] != '' ? "{$arguments['schema']}." : '' ) .
165 9
                "{$arguments['table']}'";
166 9
        } elseif (isset($arguments['schema']) && !isset($arguments['table'])) {
167 9
            $destination = "schema '{$arguments['schema']}'";
168
        }
169
170 9
        if (is_string($arguments)) {
171 1
            return $arguments;
172
        }
173
174 9
        if (isset($arguments['column'])) {
175 9
            $item = $arguments['column'];
176
        } else {
177 9
            $item = $arguments['name'];
178
        }
179
180 9
        return "'$item' $dir $destination";
181
    }
182
}