PermissionMigrateShell::processMigrationFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
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
}