Completed
Push — master ( e15c58...b150a8 )
by Changwan
07:08
created

MigrationContainer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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