Completed
Push — master ( 51d11c...72fa5d )
by Rougin
01:42
created

ChangeCommand   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 145
ccs 0
cts 55
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A current() 0 10 3
A execute() 0 20 3
A migrate() 0 10 2
A migrations() 0 16 2
A text() 0 16 4
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 Royce 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
    public function __construct(Manager $manager)
44
    {
45
        parent::__construct();
46
47
        $this->manager = $manager;
48
    }
49
50
    /**
51
     * Returns the current version of migration.
52
     *
53
     * @param  array $migrations
54
     * @return integer
55
     */
56
    protected function current($migrations)
57
    {
58
        $current = (string) $this->manager->current();
59
60
        $start = array_search($current, $migrations);
61
62
        $value = $this->reversed ? $start : $start + 1;
63
64
        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
    protected function execute(InputInterface $input, OutputInterface $output)
75
    {
76
        list($migrations, $versions, $length) = (array) $this->migrations();
77
78
        list($current, $migrated) = array($this->current($versions), false);
79
80
        for ($i = $current; $i < (integer) $length; $i++) {
81
            $version = (string) $versions[$i];
82
83
            $filename = $migrations[$version];
84
85
            $migrated = $this->migrate($output, $filename, $version);
86
        }
87
88
        if ($migrated === false) {
89
            $text = (string) 'There is nothing to migrate.';
90
91
            $output->writeln('<info>' . $text . '</info>');
92
        }
93
    }
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
    protected function migrate(OutputInterface $output, $filename, $version)
104
    {
105
        $this->text($output, $filename, true);
106
107
        $version = $this->manager->migrate($version);
108
109
        $this->text($output, $filename, false);
110
111
        return ! is_bool($version) && $version !== 1;
112
    }
113
114
    /**
115
     * Returns an array of database migrations.
116
     *
117
     * @return array
118
     */
119
    protected function migrations()
120
    {
121
        $migrations = $this->manager->migrations();
122
123
        if ($this->reversed === true) {
124
            $migrations = array_reverse($migrations);
125
126
            $migrations[0] = sys_get_temp_dir() . '';
127
        }
128
129
        $keys = (array) array_keys($migrations);
130
131
        $length = (integer) count($migrations);
132
133
        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
    protected function text(OutputInterface $output, $filename, $pending = false)
145
    {
146
        $type = $pending ? $this->pending : $this->done;
147
148
        $text = '';
149
150
        if ($filename !== sys_get_temp_dir() . '') {
151
            $parts = (array) pathinfo($filename);
152
153
            $file = $type . ' ' . $parts['filename'];
154
155
            $text = '<info>' . $file . '</info>';
156
        }
157
158
        $text !== '' && $output->writeln($text);
159
    }
160
}
161