Completed
Push — master ( 348067...ae37c1 )
by Zach
03:29 queued 01:39
created

FileDateMigrationCreator::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 2
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\Migrations\FileDate;
4
5
use Yarak\Helpers\Str;
6
use Yarak\Config\Config;
7
use Yarak\Helpers\Filesystem;
8
use Yarak\Exceptions\WriteError;
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 WriteError
78
     */
79
    protected function failIfClassExists($className)
80
    {
81
        if (class_exists($className)) {
82
            throw WriteError::classExists($className);
83
        }
84
    }
85
86
    /**
87
     * Get stub with appropriate class name/table name.
88
     *
89
     * @param string $className
90
     *
91
     * @return string
92
     */
93
    protected function getStub($className, $create)
94
    {
95
        $stubFile = $create ? 'create.stub' : 'empty.stub';
96
97
        $stub = file_get_contents(__DIR__."/../Stubs/{$stubFile}");
98
99
        return $this->populateStub($stub, $className, $create);
100
    }
101
102
    /**
103
     * Populate stub with class name and table name.
104
     *
105
     * @param string $stub
106
     * @param string $className
107
     * @param string $create
108
     *
109
     * @return string
110
     */
111
    protected function populateStub($stub, $className, $create)
112
    {
113
        if ($create) {
114
            $stub = str_replace('TABLENAME', $create, $stub);
115
        }
116
117
        return str_replace('CLASSNAME', $className, $stub);
118
    }
119
120
    /**
121
     * Get the full path to save file to.
122
     *
123
     * @param string $name
124
     *
125
     * @return string
126
     */
127
    protected function getSavePath($name)
128
    {
129
        $fileName = $this->buildFileName($name);
130
131
        return $this->config->getMigrationDirectory($fileName);
132
    }
133
134
    /**
135
     * Build file name for migration.
136
     *
137
     * @param string $name
138
     *
139
     * @return string
140
     */
141
    protected function buildFileName($name)
142
    {
143
        return $this->getDatePrefix().'_'.$name.'.php';
144
    }
145
146
    /**
147
     * Get the date prefix for the migration.
148
     *
149
     * @return string
150
     */
151
    protected function getDatePrefix()
152
    {
153
        return date('Y_m_d_His');
154
    }
155
}
156