Completed
Push — master ( c928d4...502244 )
by Viacheslav
12:21 queued 07:59
created

src/Migration/Manager.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Yaoi\Migration;
4
5
use Yaoi\Log;
6
use Yaoi\Storage;
7
use Yaoi\Service;
8
9
class Manager extends Service
10
{
11
    /**
12
     * @var Settings
13
     */
14
    protected $settings;
15
16
17
    public function isApplied($migrationId)
18
    {
19
        return (bool)$this->getStorage()->get($migrationId);
20
    }
21
22
    /**
23
     * @var Storage
24
     */
25
    private $storage;
26
27
    /**
28
     * @return Storage
29
     */
30
    protected function getStorage()
31
    {
32
        if (null === $this->storage) {
33
            $this->storage = Storage::getInstance($this->settings->storage);
34
        }
35
        return $this->storage;
36
    }
37
38
    public function perform(Migration $migration, $action = Migration::APPLY)
39
    {
40
        if ($action === Migration::SKIP) {
41
            return $this;
42
        }
43
44
        $id = $migration->getId();
45
46
        if (Migration::ROLLBACK === $action) {
47 View Code Duplication
            if ($migration->hasInternalState()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
                $migration->rollback();
49
            }
50
            elseif ($this->isApplied($id)) {
51
                if ($migration->rollback()) {
52
                    $this->getStorage()->delete($id);
53
                }
54
            }
55
56
57
        } elseif (Migration::APPLY === $action) {
58 View Code Duplication
            if ($migration->hasInternalState()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
                $migration->apply();
60
            }
61
            elseif (!$this->isApplied($id)) {
62
                if ($migration->apply()) {
63
                    $this->getStorage()->set($id, 1);
64
                }
65
            }
66
        }
67
68
        return $this;
69
    }
70
71
72
    private $jobs = array();
73
74
    /**
75
     * @param Migration[]|Migration $migrations
76
     * @param string $action
77
     * @return $this
78
     */
79
    public function add($migrations, $action = Migration::APPLY) {
80
        if ($migrations instanceof Migration) {
81
            $migrations = array($migrations);
82
        }
83
84
        foreach ($migrations as $migration) {
85
            $this->jobs []= array($migration, $action);
86
        }
87
        return $this;
88
    }
89
90
    /** @var  Log */
91
    private $log;
92
    public function setLog(Log $log = null) {
93
        $this->log = $log;
94
        return $this;
95
    }
96
97
    public function run($dryRun = false)
98
    {
99
        foreach ($this->jobs as $job) {
100
            /** @var Migration $migration */
101
            $migration = $job[0];
102
            $action = $job[1];
103
            if ($this->log) {
104
                $migration->setLog($this->log);
105
            }
106
            $migration->setDryRun($dryRun);
107
            $this->perform($migration, $action);
108
        }
109
        return $this;
110
    }
111
112
    protected static function getSettingsClassName()
113
    {
114
        return Settings::className();
115
    }
116
117
}