Completed
Push — master ( dc7866...8155ff )
by Changwan
06:46
created

MigrationContainer::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
namespace Wandu\Database\Migrator;
3
4
use SplFileInfo;
5
use Wandu\Database\Contracts\Migrator\MigrateAdapterInterface;
6
use Wandu\DI\ContainerInterface;
7
8
class MigrationContainer
9
{
10
    /** @var \SplFileInfo */
11
    protected $fileInfo;
12
13
    /** @var \Wandu\Database\Contracts\Migrator\MigrateAdapterInterface */
14
    protected $adapter;
15
    
16
    /** @var \Wandu\DI\ContainerInterface */
17
    protected $container;
18
    
19
    /**
20
     * @param \SplFileInfo $fileInfo
21
     * @param \Wandu\Database\Contracts\Migrator\MigrateAdapterInterface $adapter
22
     * @param \Wandu\DI\ContainerInterface $container
23
     */
24
    public function __construct(SplFileInfo $fileInfo, MigrateAdapterInterface $adapter, ContainerInterface $container)
25
    {
26
        $this->fileInfo = $fileInfo;
27
        $this->adapter = $adapter;
28
        $this->container = $container;
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    public function getName()
35
    {
36
        return substr($this->fileInfo->getFilename(), 14, -4);
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function getId()
43
    {
44
        return substr($this->fileInfo->getFilename(), 0, 13);
45
    }
46
47
    /**
48
     * @return bool
49
     */
50
    public function isApplied()
51
    {
52
        return !!$this->adapter->version($this->getId());
53
    }
54
    
55
    public function up()
56
    {
57
        require $this->fileInfo->__toString();
58
        $migrationName = $this->getName();
59
        $this->container->create($migrationName)->up();
60
        $this->adapter->initialize();
61
        $this->adapter->up($this->getId());
62
    }
63
    
64
    public function down()
65
    {
66
        require $this->fileInfo->__toString();
67
        $migrationName = $this->getName();
68
        $this->container->create($migrationName)->down();
69
        $this->adapter->initialize();
70
        $this->adapter->down($this->getId());
71
    }
72
}
73