Completed
Push — master ( 101dd6...834cae )
by Dmitry
05:42
created

Migration::run()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 6.0026

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 40
ccs 23
cts 24
cp 0.9583
rs 8.439
cc 6
eloc 24
nc 18
nop 2
crap 6.0026
1
<?php
2
3
namespace Basis\Job\Generate;
4
5
use Basis\Framework;
6
use Basis\Filesystem;
7
8
/**
9
 * Generate tarantool migration
10
 */
11
class Migration
12
{
13
    public $name;
14
15 2
    public function run(Filesystem $filesystem, Framework $framework)
16
    {
17 2
        $time = time();
18 2
        $namespace = date('FY', $time);
19 2
        $created_at = date('Y-m-d H:i:s', $time);
20
21 2
        if (!is_array($this->name)) {
22 2
            $this->name = explode(' ', $this->name);
23
        }
24
25 2
        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...
26
            throw new Exception("No migration name defined!");
27
        }
28
29 2
        $class = '';
30 2
        foreach ($this->name as $piece) {
31 2
            $class .= ucfirst($piece);
32
        }
33
34 2
        $template = $framework->getPath('resources/templates/migration.php');
35
36 2
        ob_start();
37 2
        include($template);
38 2
        $contents = ob_get_clean();
39
40 2
        $path = $filesystem->getPath('php/Migration');
41 2
        if (!is_dir($path)) {
42 1
            mkdir($path);
43
        }
44
45 2
        $path = $filesystem->getPath('php/Migration/'.$namespace);
46 2
        if (!is_dir($path)) {
47 2
            mkdir($path);
48
        }
49
50 2
        $filename = 'php/Migration/'.$namespace.'/'.$class.'.php';
51 2
        file_put_contents($filename, $contents);
52
53 2
        return compact('filename', 'namespace', 'class');
54
    }
55
}
56