Completed
Push — dev ( 6bece5...cad8d6 )
by Zach
02:11
created

FileDateMigrationCreator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 128
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 5

8 Methods

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