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

MigrationContainer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getName() 0 4 1
A getId() 0 4 1
A isApplied() 0 4 1
A up() 0 8 1
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