1 | <?php |
||
2 | namespace yentu\commands; |
||
3 | |||
4 | |||
5 | use yentu\factories\DatabaseManipulatorFactory; |
||
6 | use yentu\Migrations; |
||
7 | use clearice\io\Io; |
||
8 | |||
9 | |||
10 | class Status extends Command |
||
11 | { |
||
12 | private $manipulatorFactory; |
||
13 | private $migrations; |
||
14 | private $io; |
||
15 | |||
16 | public function __construct(Migrations $migrations, DatabaseManipulatorFactory $manipulatorFactory, Io $io) |
||
17 | { |
||
18 | $this->io = $io; |
||
19 | $this->manipulatorFactory = $manipulatorFactory; |
||
20 | $this->migrations = $migrations; |
||
21 | } |
||
22 | |||
23 | public function run() |
||
24 | { |
||
25 | $driver = $this->manipulatorFactory->createManipulator(); |
||
26 | $version = $driver->getVersion(); |
||
27 | |||
28 | if ($version == null) { |
||
29 | $this->io->output("\nYou have not applied any migrations\n"); |
||
30 | return; |
||
31 | } |
||
32 | |||
33 | $migrationInfo = $this->getMigrationInfo(); |
||
34 | |||
35 | $this->io->output("\n" . ($migrationInfo['counter']['previous'] == 0 ? 'No' : $migrationInfo['counter']['previous']) . " migration(s) have been applied so far.\n"); |
||
36 | $this->displayMigrations($migrationInfo['run']['previous']); |
||
37 | |||
38 | $this->io->output("\nLast migration applied:\n {$migrationInfo['current']}\n"); |
||
39 | |||
40 | if ($migrationInfo['counter']['yet'] > 0) { |
||
41 | $this->io->output("\n{$migrationInfo['counter']['yet']} migration(s) that could be applied.\n"); |
||
42 | $this->displayMigrations($migrationInfo['run']['yet']); |
||
43 | } else { |
||
44 | $this->io->output("\nThere are no pending migrations.\n"); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | private function getMigrationInfo() |
||
49 | { |
||
50 | $runMigrations = $this->migrations->getRunMirations(); |
||
51 | $migrations = $this->migrations->getAllMigrations(); |
||
52 | |||
53 | $counter['previous'] = count($runMigrations); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
54 | end($runMigrations); |
||
55 | $current = "{$runMigrations[key($runMigrations)]['timestamp']} {$runMigrations[key($runMigrations)]['migration']}"; |
||
56 | $run = array( |
||
57 | 'previous' => array(), |
||
58 | 'yet' => array() |
||
59 | ); |
||
60 | |||
61 | foreach ($runMigrations as $timestamp => $migration) { |
||
62 | unset($migrations[$timestamp]); |
||
63 | $run['previous'][] = "{$timestamp} {$migration['migration']} " . |
||
64 | ($migration['default_schema'] == '' ? '' : "on `{$migration['default_schema']}` schema"); |
||
65 | } |
||
66 | |||
67 | foreach ($migrations as $timestamp => $migration) { |
||
68 | $run['yet'][] = "{$timestamp} {$migration['migration']}"; |
||
69 | } |
||
70 | $counter['yet'] = count($run['yet']); |
||
71 | |||
72 | return array( |
||
73 | 'counter' => $counter, |
||
74 | 'current' => $current, |
||
75 | 'run' => $run |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | private function displayMigrations($descriptions) |
||
80 | { |
||
81 | foreach ($descriptions as $description) { |
||
82 | if (trim($description) == '') { |
||
83 | continue; |
||
84 | } |
||
85 | $this->io->output(" $description\n"); |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 |