PublishSeeds::copySeed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Larafolio\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
8
class PublishSeeds extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'larafolio:seeds';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Move Larafolio seeders into local project';
23
24
    /**
25
     * Normal seed files to copy.
26
     *
27
     * @var array
28
     */
29
    protected $seeds = [
30
        'ProjectsTableSeeder.php',
31
        'TextBlocksTableSeeder.php',
32
        'UsersTableSeeder.php',
33
    ];
34
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return mixed
39
     */
40
    public function handle()
41
    {
42
        $filesystem = new Filesystem();
43
44
        if (!$filesystem->exists($this->seedPath())) {
45
            $filesystem->makeDirectory($this->seedPath());
46
47
            $this->info('Larafolio directory created.');
48
        }
49
50
        $this->copyRootSeeder($filesystem);
51
52
        $this->copyImageSeeder($filesystem);
53
54
        $this->copyFactories($filesystem);
55
56
        foreach ($this->seeds as $seed) {
57
            $this->copySeed($filesystem, $seed);
58
        }
59
    }
60
61
    /**
62
     * Copy the root database seeder class to the app.
63
     *
64
     * @param \Illuminate\Filesystem\Filesystem $filesystem
65
     */
66 View Code Duplication
    protected function copyRootSeeder(Filesystem $filesystem)
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...
67
    {
68
        $file = $filesystem->get(__DIR__.'/../database/seeds/DatabaseSeeder.php');
69
70
        $file = $this->removeNamespace($file);
71
72
        $file = str_replace('DatabaseSeeder', 'LarafolioSeeder', $file);
73
74
        $filesystem->put($this->seedPath('LarafolioSeeder.php'), $file);
75
    }
76
77
    /**
78
     * Copy the image table seeder class to the app.
79
     *
80
     * @param \Illuminate\Filesystem\Filesystem $filesystem
81
     */
82 View Code Duplication
    protected function copyImageSeeder(Filesystem $filesystem)
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...
83
    {
84
        $file = $filesystem->get(__DIR__.'/../database/seeds/ImagesTableSeeder.php');
85
86
        $file = $this->removeNamespace($file);
87
88
        $file = str_replace('/../../../tests/_data/images',
89
            '/../../vendor/zachleigh/larafolio/tests/_data/images',
90
            $file
91
        );
92
93
        $filesystem->put($this->seedPath('ImagesTableSeeder.php'), $file);
94
    }
95
96
    /**
97
     * Copy standard seed to the app.
98
     *
99
     * @param \Illuminate\Filesystem\Filesystem $filesystem
100
     * @param string                            $seed       Seed name.
101
     */
102
    protected function copySeed(Filesystem $filesystem, $seed)
103
    {
104
        $file = $filesystem->get(__DIR__."/../database/seeds/{$seed}");
105
106
        $file = $this->removeNamespace($file);
107
108
        $filesystem->put($this->seedPath($seed), $file);
109
    }
110
111
    /**
112
     * Return database seed path plus any file name.
113
     *
114
     * @param string $fileName Name of file to append to path.
115
     *
116
     * @return string
117
     */
118
    protected function seedPath($fileName = null)
119
    {
120
        if ($fileName === null) {
121
            return database_path('seeds');
122
        }
123
124
        return database_path('seeds/').$fileName;
125
    }
126
127
    /**
128
     * Remove namespace from seeder file.
129
     *
130
     * @param string $file File contents.
131
     *
132
     * @return string
133
     */
134
    protected function removeNamespace($file)
135
    {
136
        return str_replace("namespace Larafolio\database\seeds;\n\n", '', $file);
137
    }
138
139
    /**
140
     * Copy model factories to app.
141
     *
142
     * @param \Illuminate\Filesystem\Filesystem $filesystem
143
     */
144
    public function copyFactories(Filesystem $filesystem)
145
    {
146
        $file = $filesystem->get(__DIR__.'/../database/factories/ModelFactory.php');
147
148
        $filesystem->put(database_path('factories/ModelFactory.php'), $file);
149
    }
150
}
151