Completed
Push — dev ( 348067...b33ea3 )
by Zach
02:21
created

FileDateMigrationCreator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 147
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 3

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 18 1
A getClassName() 0 4 1
A failIfClassExists() 0 8 2
A getStub() 0 8 2
A populateStub() 0 8 2
A getSavePath() 0 6 1
A buildFileName() 0 4 1
A getDatePrefix() 0 4 1
1
<?php
2
3
namespace Yarak\Migrations\FileDate;
4
5
use Phalcon\Exception;
6
use Yarak\Helpers\Str;
7
use Yarak\Config\Config;
8
use Yarak\Helpers\Filesystem;
9
use Yarak\Migrations\MigrationCreator;
10
11
class FileDateMigrationCreator implements MigrationCreator
12
{
13
    use Filesystem;
14
15
    /**
16
     * Yarak config.
17
     *
18
     * @var Config
19
     */
20
    protected $config;
21
22
    /**
23
     * Construct.
24
     *
25
     * @param Config $config
26
     */
27
    public function __construct(Config $config)
28
    {
29
        $this->config = $config;
30
    }
31
32
    /**
33
     * Create a migration file.
34
     *
35
     * @param string $name
36
     * @param string $create
37
     *
38
     * @return string
39
     */
40
    public function create($name, $create = false)
41
    {
42
        $className = $this->getClassName($name);
43
44
        $this->failIfClassExists($className);
45
46
        $this->makeDirectoryStructure([
47
            $this->config->getDatabaseDirectory(),
48
            $this->config->getMigrationDirectory(),
49
        ]);
50
51
        $this->writeFile(
52
            $path = $this->getSavePath($name),
53
            $this->getStub($className, $create)
54
        );
55
56
        return $path;
57
    }
58
59
    /**
60
     * Return StudlyCase class name.
61
     *
62
     * @param string $name
63
     *
64
     * @return string
65
     */
66
    protected function getClassName($name)
67
    {
68
        return Str::studly($name);
69
    }
70
71
    /**
72
     * If class name already exists, throw exception. Prone to failure due to
73
     * autoloading strategy.
74
     *
75
     * @param string $className
76
     *
77
     * @throws Phalcon\Exception
78
     */
79
    protected function failIfClassExists($className)
80
    {
81
        if (class_exists($className)) {
82
            throw new Exception(
83
                sprintf('Class "%s" already exists.', $className)
84
            );
85
        }
86
    }
87
88
    /**
89
     * Get stub with appropriate class name/table name.
90
     *
91
     * @param string $className
92
     *
93
     * @return string
94
     */
95
    protected function getStub($className, $create)
96
    {
97
        $stubFile = $create ? 'create.stub' : 'empty.stub';
98
99
        $stub = file_get_contents(__DIR__."/../Stubs/{$stubFile}");
100
101
        return $this->populateStub($stub, $className, $create);
102
    }
103
104
    /**
105
     * Populate stub with class name and table name.
106
     *
107
     * @param string $stub
108
     * @param string $className
109
     * @param string $create
110
     *
111
     * @return string
112
     */
113
    protected function populateStub($stub, $className, $create)
114
    {
115
        if ($create) {
116
            $stub = str_replace('TABLENAME', $create, $stub);
117
        }
118
119
        return str_replace('CLASSNAME', $className, $stub);
120
    }
121
122
    /**
123
     * Get the full path to save file to.
124
     *
125
     * @param string $name
126
     *
127
     * @return string
128
     */
129
    protected function getSavePath($name)
130
    {
131
        $fileName = $this->buildFileName($name);
132
133
        return $this->config->getMigrationDirectory($fileName);
134
    }
135
136
    /**
137
     * Build file name for migration.
138
     *
139
     * @param string $name
140
     *
141
     * @return string
142
     */
143
    protected function buildFileName($name)
144
    {
145
        return $this->getDatePrefix().'_'.$name.'.php';
146
    }
147
148
    /**
149
     * Get the date prefix for the migration.
150
     *
151
     * @return string
152
     */
153
    protected function getDatePrefix()
154
    {
155
        return date('Y_m_d_His');
156
    }
157
}
158