BuildCommand::parseMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
/**
4
 * Class BuildCommand.
5
 */
6
class BuildCommand extends SilverstripeCommand
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    /**
9
     * @var string
10
     */
11
    protected $name = 'dev:build';
12
13
    /**
14
     * @var string
15
     */
16
    protected $description = 'Rebuild the database';
17
18
    CONST CHANGE  = '+';
19
    CONST DELETE  = '-';
20
    CONST NOTICE  = '*';
21
    CONST ERROR   = '!';
22
23
    public function fire()
24
    {
25
        // make sure we have a fresh manifest
26
        $this->call('clear:cache');
27
28
        /** @var DatabaseAdmin $da */
29
        $da = DatabaseAdmin::create();
30
31
        // hack untill we have something better...
32
        ob_start();
33
        $da->doBuild();
34
        $content = ob_get_contents();
35
        ob_get_clean();
36
        foreach(explode("\n", $content) as $line) {
37
            $this->displayBuildMessage($line);
38
        }
39
    }
40
41
    protected function displayBuildMessage($buildMessage)
42
    {
43
        $message = trim($buildMessage);
44
45
        $change = BuildCommand::CHANGE;
46
        $delete = BuildCommand::DELETE;
47
        $notice = BuildCommand::NOTICE;
48
        $error  = BuildCommand::ERROR;
49
50
        if(Str::startsWith($message, $change)) {
51
            $this->info($this->parseMessage($message, $change));
52
        }elseif(Str::startsWith($message, $delete)) {
53
            $this->warn($this->parseMessage($message, $delete));
54
        }elseif(Str::startsWith($message, $notice)) {
55
            $this->comment($this->parseMessage($message, $notice));
56
        }elseif(Str::startsWith($message, $error)) {
57
            $this->error($this->parseMessage($message, $error));
58
        }elseif($message){
59
            $this->line($this->parseMessage($message));
60
        }
61
    }
62
63
    /**
64
     * @param string $message
65
     * @param string$replace
66
     * @return string
67
     */
68
    protected function parseMessage($message, $replace = '')
69
    {
70
        if($replace) {
71
            $message = Str::replaceFirst($replace, '', $message);
72
        }
73
74
        return trim($message);
75
    }
76
}
77