FileDateMigrationCreator::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 15
Ratio 68.18 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 15
loc 22
rs 9.568
c 0
b 0
f 0
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 View Code Duplication
        if (!class_exists($className)) {
25
            $this->makeDirectoryStructure([
26
                'database'   => $this->config->getDatabaseDirectory(),
27
                'migrations' => $this->config->getMigrationDirectory(),
28
            ], $this->output);
29
30
            $this->writeFile(
31
                $path = $this->getSavePath($name),
32
                $this->getStub($className, $create)
33
            );
34
35
            $this->output->writeInfo("Created migration {$name}.");
36
37
            return $path;
38
        }
39
40
        throw WriteError::classExists($className);
41
    }
42
43
    /**
44
     * Return StudlyCase class name.
45
     *
46
     * @param string $name
47
     *
48
     * @return string
49
     */
50
    protected function getClassName($name)
51
    {
52
        return Str::studly($name);
53
    }
54
55
    /**
56
     * Get stub with appropriate class name/table name.
57
     *
58
     * @param string $className
59
     *
60
     * @return string
61
     */
62
    protected function getStub($className, $create)
63
    {
64
        $stubFile = $create ? 'create.stub' : 'empty.stub';
65
66
        $stub = file_get_contents(__DIR__."/../Stubs/{$stubFile}");
67
68
        return $this->populateStub($stub, $className, $create);
69
    }
70
71
    /**
72
     * Populate stub with class name and table name.
73
     *
74
     * @param string $stub
75
     * @param string $className
76
     * @param string $create
77
     *
78
     * @return string
79
     */
80
    protected function populateStub($stub, $className, $create)
81
    {
82
        if ($create) {
83
            $stub = str_replace('TABLENAME', $create, $stub);
84
        }
85
86
        return str_replace('CLASSNAME', $className, $stub);
87
    }
88
89
    /**
90
     * Get the full path to save file to.
91
     *
92
     * @param string $name
93
     *
94
     * @return string
95
     */
96
    protected function getSavePath($name)
97
    {
98
        $fileName = $this->buildFileName($name);
99
100
        return $this->config->getMigrationDirectory($fileName);
101
    }
102
103
    /**
104
     * Build file name for migration.
105
     *
106
     * @param string $name
107
     *
108
     * @return string
109
     */
110
    protected function buildFileName($name)
111
    {
112
        return $this->getDatePrefix().'_'.$name.'.php';
113
    }
114
115
    /**
116
     * Get the date prefix for the migration.
117
     *
118
     * @return string
119
     */
120
    protected function getDatePrefix()
121
    {
122
        return date('Y_m_d_His');
123
    }
124
}
125