Completed
Push — dev ( 69ff8f...3ddbd6 )
by Zach
02:18
created

FileDateMigrationCreator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 127
Duplicated Lines 8.66 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 19 2
A getClassName() 0 4 1
A createDirectories() 11 11 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

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        if (!class_exists($className)) {
25
            $this->createDirectories();
26
27
            $this->writeFile(
28
                $path = $this->getSavePath($name),
29
                $this->getStub($className, $create)
30
            );
31
32
            $this->output->writeInfo("Created migration {$name}.");
33
34
            return $path;
35
        }
36
37
        throw WriteError::classExists($className);
38
    }
39
40
    /**
41
     * Return StudlyCase class name.
42
     *
43
     * @param string $name
44
     *
45
     * @return string
46
     */
47
    protected function getClassName($name)
48
    {
49
        return Str::studly($name);
50
    }
51
52
    /**
53
     * Create necessary directories for migrations.
54
     */
55 View Code Duplication
    protected function createDirectories()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $created = $this->makeDirectoryStructure([
58
            'database'   => $this->config->getDatabaseDirectory(),
59
            'migrations' => $this->config->getMigrationDirectory(),
60
        ]);
61
62
        foreach ($created as $key => $value) {
63
            $this->output->writeInfo("Created {$key} directory.");
64
        }
65
    }
66
67
    /**
68
     * Get stub with appropriate class name/table name.
69
     *
70
     * @param string $className
71
     *
72
     * @return string
73
     */
74
    protected function getStub($className, $create)
75
    {
76
        $stubFile = $create ? 'create.stub' : 'empty.stub';
77
78
        $stub = file_get_contents(__DIR__."/../Stubs/{$stubFile}");
79
80
        return $this->populateStub($stub, $className, $create);
81
    }
82
83
    /**
84
     * Populate stub with class name and table name.
85
     *
86
     * @param string $stub
87
     * @param string $className
88
     * @param string $create
89
     *
90
     * @return string
91
     */
92
    protected function populateStub($stub, $className, $create)
93
    {
94
        if ($create) {
95
            $stub = str_replace('TABLENAME', $create, $stub);
96
        }
97
98
        return str_replace('CLASSNAME', $className, $stub);
99
    }
100
101
    /**
102
     * Get the full path to save file to.
103
     *
104
     * @param string $name
105
     *
106
     * @return string
107
     */
108
    protected function getSavePath($name)
109
    {
110
        $fileName = $this->buildFileName($name);
111
112
        return $this->config->getMigrationDirectory($fileName);
113
    }
114
115
    /**
116
     * Build file name for migration.
117
     *
118
     * @param string $name
119
     *
120
     * @return string
121
     */
122
    protected function buildFileName($name)
123
    {
124
        return $this->getDatePrefix().'_'.$name.'.php';
125
    }
126
127
    /**
128
     * Get the date prefix for the migration.
129
     *
130
     * @return string
131
     */
132
    protected function getDatePrefix()
133
    {
134
        return date('Y_m_d_His');
135
    }
136
}
137