Completed
Push — dev ( fef52a...7941f9 )
by Darko
07:16
created

UpdateNNTmux::handle()   B

Complexity

Conditions 8
Paths 104

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 38
ccs 0
cts 22
cp 0
rs 8.4111
c 0
b 0
f 0
cc 8
nc 104
nop 0
crap 72
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Blacklight\Tmux;
6
use Illuminate\Console\Command;
7
use Ytake\LaravelSmarty\Smarty;
8
use Illuminate\Support\Facades\App;
9
10
class UpdateNNTmux extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'nntmux:all';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Update NNTmux installation';
25
26
    /**
27
     * @var \app\extensions\util\Git object.
28
     */
29
    protected $git;
30
31
    /**
32
     * @var array Decoded JSON updates file.
33
     */
34
    protected $updates = null;
35
36
    /**
37
     * Create a new command instance.
38
     */
39
    public function __construct()
40
    {
41
        parent::__construct();
42
    }
43
44
    /**
45
     * Execute the console command.
46
     */
47
    public function handle(): void
48
    {
49
        $maintenance = $this->appDown();
50
        $running = $this->stopTmux();
51
52
        try {
53
            $output = $this->call('nntmux:git');
54
            if ($output === 'Already up-to-date.') {
0 ignored issues
show
introduced by
The condition $output === 'Already up-to-date.' is always false.
Loading history...
55
                $this->info($output);
56
            } else {
57
                $status = $this->call('nntmux:composer');
58
                if ($status) {
59
                    $this->error('Composer failed to update!!');
60
                }
61
                $fail = $this->call('nntmux:db');
62
                if ($fail) {
63
                    $this->error('Db updating failed!!');
64
                }
65
            }
66
        } catch (\Exception $e) {
67
            $this->error($e->getMessage());
68
        }
69
70
        $cleared = (new Smarty())->setCompileDir(config('ytake-laravel-smarty.compile_path'))->clearCompiledTemplate();
71
        if ($cleared) {
72
            $this->output->writeln('<comment>The Smarty compiled template cache has been cleaned for you</comment>');
73
        } else {
74
            $this->output->writeln(
75
                '<comment>You should clear your Smarty compiled template cache at: '.
76
                config('ytake-laravel-smarty.compile_path').'</comment>'
77
            );
78
        }
79
80
        if ($maintenance === true) {
81
            $this->call('up');
82
        }
83
        if ($running === true) {
84
            $this->startTmux();
85
        }
86
    }
87
88
    /**
89
     * @return bool
90
     */
91
    private function appDown(): bool
92
    {
93
        if ($this->appDown() === false) {
0 ignored issues
show
introduced by
The condition $this->appDown() === false is always true.
Loading history...
94
            $this->call('down');
95
96
            return true;
97
        }
98
99
        return false;
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    private function stopTmux(): bool
106
    {
107
        if ((new Tmux())->isRunning() === true) {
108
            $this->call('tmux-ui:stop --kill');
109
110
            return true;
111
        }
112
113
        return false;
114
    }
115
116
    private function startTmux(): void
117
    {
118
        $this->call('tmux-ui:start');
119
    }
120
}
121