ChangeCommand::text()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 8
nop 3
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 4
rs 10
1
<?php
2
3
namespace Rougin\Refinery\Console;
4
5
use Rougin\Refinery\Manager;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Change Command
12
 *
13
 * @package Refinery
14
 * @author  Rougin Gutib <[email protected]>
15
 */
16
class ChangeCommand extends Command
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $done = 'Migrated:  ';
22
23
    /**
24
     * @var \Rougin\Refinery\Manager
25
     */
26
    protected $manager;
27
28
    /**
29
     * @var string
30
     */
31
    protected $pending = 'Migrating: ';
32
33
    /**
34
     * @var boolean
35
     */
36
    protected $reversed = false;
37
38
    /**
39
     * Initializes the command instance.
40
     *
41
     * @param \Rougin\Refinery\Manager $manager
42
     */
43 24
    public function __construct(Manager $manager)
44
    {
45 24
        parent::__construct();
46
47 24
        $this->manager = $manager;
48 24
    }
49
50
    /**
51
     * Returns the current version of migration.
52
     *
53
     * @param  array $migrations
54
     * @return integer
55
     */
56 9
    protected function current($migrations)
57
    {
58 9
        $current = (string) $this->manager->current();
59
60 9
        $start = array_search($current, $migrations);
61
62 9
        $value = $this->reversed ? $start : $start + 1;
63
64 9
        return $start === false ? 0 : (integer) $value;
65
    }
66
67
    /**
68
     * Executes the command.
69
     *
70
     * @param  \Symfony\Component\Console\Input\InputInterface   $input
71
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
72
     * @return void
73
     */
74 9
    protected function execute(InputInterface $input, OutputInterface $output)
75
    {
76 9
        list($migrations, $versions, $length) = (array) $this->migrations();
77
78 9
        list($current, $migrated) = array($this->current($versions), false);
79
80 9
        for ($i = $current; $i < (integer) $length; $i++) {
81 6
            $version = (string) $versions[$i];
82
83 6
            $filename = $migrations[$version];
84
85 6
            $migrated = $this->migrate($output, $filename, $version);
86 2
        }
87
88 9
        if ($migrated === false) {
89 3
            $text = (string) 'There is nothing to migrate.';
90
91 3
            $output->writeln('<info>' . $text . '</info>');
92 1
        }
93 9
    }
94
95
    /**
96
     * Migrates the database to a specified schema version.
97
     *
98
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
99
     * @param  string                                            $filename
100
     * @param  string                                            $version
101
     * @return boolean
102
     */
103 12
    protected function migrate(OutputInterface $output, $filename, $version)
104
    {
105 12
        $this->text($output, $filename, true);
106
107 12
        $version = $this->manager->migrate($version);
108
109 12
        $this->text($output, $filename, false);
110
111 12
        return ! is_bool($version) && $version !== 1;
112
    }
113
114
    /**
115
     * Returns an array of database migrations.
116
     *
117
     * @return array
118
     */
119 15
    protected function migrations()
120
    {
121 15
        $migrations = $this->manager->migrations();
122
123 15
        if ($this->reversed === true) {
124 9
            $migrations = array_reverse($migrations);
125
126 9
            $migrations[0] = sys_get_temp_dir() . '';
127 3
        }
128
129 15
        $keys = (array) array_keys($migrations);
130
131 15
        $length = (integer) count($migrations);
132
133 15
        return array($migrations, $keys, $length);
134
    }
135
136
    /**
137
     * Outputs a text based on filename from a database migration.
138
     *
139
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
140
     * @param  string                                            $filename
141
     * @param  boolean                                           $pending
142
     * @return void
143
     */
144 12
    protected function text(OutputInterface $output, $filename, $pending = false)
145
    {
146 12
        $type = $pending ? $this->pending : $this->done;
147
148 12
        $text = '';
149
150 12
        if ($filename !== sys_get_temp_dir() . '') {
151 12
            $parts = (array) pathinfo($filename);
152
153 12
            $file = $type . ' ' . $parts['filename'];
154
155 12
            $text = '<info>' . $file . '</info>';
156 4
        }
157
158 12
        $text !== '' && $output->writeln($text);
159 12
    }
160
}
161