Completed
Push — master ( a59137...77647e )
by Chubarov
02:27
created

MigrateLaravelCommand::execute()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 27
ccs 0
cts 0
cp 0
crap 2
rs 8.8571
1
<?php
2
namespace agoalofalife\Commands;
3
4
5
use Carbon\Carbon;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
8
use Symfony\Component\Console\Helper\ProgressBar;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class MigrateLaravelCommand extends Command
13
{
14
    protected $listFileMigrations = [
15
        'country' => '_create_table_country',
16
        'regions' => '_create_table_regions',
17
        'cities'  => '_create_table_cities'
18
    ];
19
20
    protected $listFilesSeeder = [
21
      'CountryTableSeeder' => 'CountryTableSeeder.php',
22
      'RegionsTableSeeder' => 'RegionsTableSeeder.php',
23
      'CitiesTableSeeder'  => 'CitiesTableSeeder.php'
24
    ];
25
26
    protected $formatterStyle;
27
    protected $progressBar;
28
29 2
    public function __construct(OutputFormatterStyle $formatterStyle, ProgressBar $bar)
30
    {
31 2
        parent::__construct();
32 2
        $this->formatterStyle = $formatterStyle;
33 2
        $this->progressBar    = $bar;
34 2
        $this->progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%');
35 2
    }
36
37 2
    protected function configure()
38
    {
39 2
        $this->setName('migrate:laravel')->setHelp('to migrate files to Laravel');
40 2
    }
41
42
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Coding Style introduced by
execute uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
43
    {
44
        $output->getFormatter()->setStyle('fire', $this->formatterStyle);
45
46
        $output->writeln([
47
            '<fire>There is a migration in the project Laravel</fire>',
48
            ''
49
        ]);
50
        
51
        $this->progressBar->start();
52
53
        $this->move( $this->progressBar, [
54
            'inLaravel' => $_SERVER["PWD"] . '/database/migrations/',
55
            'stubs'     => __DIR__ . '/../database/migrations/stubs/'
56
        ], $this->listFileMigrations);
57
58
        $this->move( $this->progressBar, [
59
            'inLaravel' => $_SERVER["PWD"] . '/database/seeds/',
60
            'stubs'     => __DIR__ . '/../database/seeds/stubs/'
61
        ], $this->listFilesSeeder );
62
63
        $this->moveConfig(  $this->progressBar );
64
65
        $this->progressBar->finish();
66
        $output->writeln(['']);
67
        $output->writeln(['<info>All successfully copied!</info>']);
68
    }
69
70
    protected function move(ProgressBar $progress, array $pathList, $fileData)
71
    {
72
        $this->createDir($pathList['inLaravel']);
73
74
        foreach ($fileData as $name => $migrate)
75
        {
76
            $fileName = $pathList['inLaravel'] . $this->getDateNormalize() . $migrate . '.php';
77
            file_put_contents($fileName, $this->getContent($pathList['stubs'] . $name));
78
            $progress->advance();
79
        }
80
    }
81
82
83
    protected function moveConfig(ProgressBar $progress)
0 ignored issues
show
Coding Style introduced by
moveConfig uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
84
    {
85
        $pathToConfig               = __DIR__ . '/../config.php';
86
        $pathToConfigsLaravel       =  $_SERVER["PWD"] . '/config/';
87
        $this->createDir($pathToConfigsLaravel);
88
89
        copy($pathToConfig,$pathToConfigsLaravel . 'geography.php' );
90
        $progress->advance();
91
    }
92
93
    /**
94
     * Just create directory
95
     * @param $dir
96
     */
97
    protected function createDir($dir)
98
    {
99
        if (is_dir($dir) === false)
100
        {
101
            mkdir($dir, 0777, true);
102
        }
103
    }
104
105
    /**
106
     * Data from stubs file
107
     * @param $nameFile
108
     * @return bool|string
109
     */
110
    protected function getContent($nameFile)
111
    {
112
        return file_get_contents($nameFile);
113
    }
114
115
    /**
116
     * Get date normalize mow
117
     * @return mixed
118
     */
119
    protected function getDateNormalize()
120
    {
121
        $date = Carbon::now();
122
        $date = preg_replace('/-|\s/','_', $date);
123
        $data = preg_replace('/:/','', $date);
124
125
        return $data;
126
    }
127
}