PermissionMigrateShell::main()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.2
cc 4
eloc 9
nc 6
nop 0
1
<?php
2
/**
3
 * CakePHP permission handling library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\CakePermission\Shell;
7
8
use Cake\Console\Shell;
9
use Slince\CakePermission\Exception\RuntimeException;
10
11
class PermissionMigrateShell extends Shell
12
{
13
14
    protected $migrationFilesPath;
15
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function main()
20
    {
21
        $this->migrationFilesPath = CONFIG . 'Migrations/';
22
        //Creates the directory if it does not exists
23
        file_exists($this->migrationFilesPath) || mkdir($this->migrationFilesPath, 0777, true);
24
        //All migration files
25
        $srcMigrationFiles = $this->findMigrationFiles(__DIR__ . '/../../database/migrations');
26
        foreach ($srcMigrationFiles as $file) {
27
            $result = $this->processMigrationFile($file);
28
            if (!$result) {
29
                throw new RuntimeException(sprintf('Fail to copy migration file "%s"', $file));
30
            }
31
        }
32
        $this->out('Migrate ok, please execute command "./cake migrations migrate"');
33
    }
34
35
    /**
36
     * Find array of migration file
37
     * @param string $path
38
     * @return array
39
     */
40
    protected function findMigrationFiles($path)
41
    {
42
        return glob("{$path}/*");
43
    }
44
45
    protected function processMigrationFile($originFile)
46
    {
47
        $format = 'YmdHis';
48
        $newFileName = $this->migrationFilesPath
49
            . date($format) . '_' . basename($originFile);
50
        return copy($originFile, $newFileName);
51
    }
52
}