Rollback   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
eloc 35
dl 0
loc 63
rs 10
c 6
b 1
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getOperations() 0 13 3
A run() 0 28 6
A __construct() 0 5 1
1
<?php
2
3
namespace yentu\commands;
4
5
use yentu\ChangeReverser;
6
use yentu\factories\DatabaseManipulatorFactory;
7
use clearice\io\Io;
8
9
class Rollback extends Command
10
{
11
12
//    private $schemaCondition;
13
    private $schemaConditionData = [];
14
    private $manipulatorFactory;
15
    private $io;
16
    private $changeReverser;
17
18
    public function __construct(DatabaseManipulatorFactory $manipulatorFactory, Io $io, ChangeReverser $changeReverser)
19
    {
20
        $this->manipulatorFactory = $manipulatorFactory;
21
        $this->io = $io;
22
        $this->changeReverser = $changeReverser;
23
    }
24
25
    /**
26
     * @throws \yentu\exceptions\DatabaseManipulatorException
27
     */
28
    public function run()
29
    {
30
        $db = $this->manipulatorFactory->createManipulator();
31
        $this->changeReverser->setDriver($db);
32
        $previousMigration = '';
33
        
34
        if (isset($this->options['__args'])) {
35
            $operations = [];
36
            foreach ($this->options['__args'] ?? [] as $set) {
37
                $operations += $this->getOperations($db, $set);
38
            }
39
        } else {
40
            $session = $db->getLastSession();
41
            $operations = $db->query(
42
                "SELECT id, method, arguments, migration, default_schema FROM yentu_history WHERE session = ? ORDER BY id DESC", $this->schemaConditionData + [$session]
43
            );
44
        }
45
46
        foreach ($operations as $operation) {
47
            if ($previousMigration !== $operation['migration']) {
48
                $this->io->output(
49
                    "Rolling back '{$operation['migration']}' migration" .
50
                    ($operation['default_schema'] != '' ? " on `{$operation['default_schema']}` schema." : ".") . "\n"
51
                );
52
                $previousMigration = $operation['migration'];
53
            }
54
            $this->changeReverser->call($operation['method'], json_decode($operation['arguments'], true));
55
            $db->query('DELETE FROM yentu_history WHERE id = ?', array($operation['id']));
56
        }
57
    }
58
59
    private function getOperations($db, $set)
60
    {
61
        $operations = [];
62
        if (preg_match("/[0-9]{14}/", $set)) {
63
            $operations = $db->query(
64
                "SELECT id, method, arguments, migration, default_schema FROM yentu_history WHERE version = ? ORDER by id DESC", $this->schemaConditionData + [$set]
65
            );
66
        } elseif (preg_match("/[a-zA-Z\_\-]+/", $set)) {
67
            $operations = $db->query(
68
                "SELECT id, method, arguments, migration, default_schema FROM yentu_history WHERE migration = ? ORDER by id DESC", $this->schemaConditionData + [$set]
69
            );
70
        }
71
        return $operations;
72
    }
73
74
}
75