Completed
Push — master ( 692570...6b6f8f )
by Dmitry
03:35
created

Migration::run()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6.0208

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 40
ccs 22
cts 24
cp 0.9167
rs 8.439
c 1
b 0
f 0
cc 6
eloc 24
nc 18
nop 2
crap 6.0208
1
<?php
2
3
namespace Basis\Job\Generate;
4
5
use Basis\Filesystem;
6
use Basis\Framework;
7
use Basis\Job;
8
9
/**
10
 * Generate tarantool migration
11
 */
12
class Migration extends Job
13
{
14
    public $name;
15
16 1
    public function run(Filesystem $filesystem, Framework $framework)
17
    {
18 1
        $time = time();
19 1
        $namespace = date('FY', $time);
20 1
        $created_at = date('Y-m-d H:i:s', $time);
21
22 1
        if (!is_array($this->name)) {
23 1
            $this->name = explode(' ', $this->name);
24
        }
25
26 1
        if (!$this->name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->name of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
27
            throw new Exception("No migration name defined!");
28
        }
29
30 1
        $class = '';
31 1
        foreach ($this->name as $piece) {
32 1
            $class .= ucfirst($piece);
33
        }
34
35 1
        $template = $framework->getPath('resources/templates/migration.php');
36
37 1
        ob_start();
38 1
        include($template);
39 1
        $contents = ob_get_clean();
40
41 1
        $path = $filesystem->getPath('php/Migration');
42 1
        if (!is_dir($path)) {
43
            mkdir($path);
44
        }
45
46 1
        $path = $filesystem->getPath('php/Migration/'.$namespace);
47 1
        if (!is_dir($path)) {
48 1
            mkdir($path);
49
        }
50
51 1
        $filename = 'php/Migration/'.$namespace.'/'.$class.'.php';
52 1
        file_put_contents($filename, $contents);
53
54 1
        return compact('filename', 'namespace', 'class');
55
    }
56
}
57