Passed
Push — master ( 0ddaf0...97ef82 )
by Darko
07:24
created

UpdateNNTmuxComposer::handle()   A

Complexity

Conditions 4
Paths 17

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 32
rs 9.6333
c 0
b 0
f 0
cc 4
nc 17
nop 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\File;
7
use Illuminate\Support\Facades\Process;
8
9
class UpdateNNTmuxComposer extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'nntmux:composer
17
                            {--no-dev : Skip development dependencies}
18
                            {--optimize : Optimize autoloader}
19
                            {--prefer-dist : Prefer distribution packages}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Update composer dependencies with optimizations';
27
28
    /**
29
     * Execute the console command.
30
     */
31
    public function handle(): int
32
    {
33
        try {
34
            $this->info('📦 Starting composer update process...');
35
36
            // Check if composer.json exists
37
            if (!File::exists(base_path('composer.json'))) {
38
                $this->error('composer.json not found');
39
                return Command::FAILURE;
40
            }
41
42
            // Check if composer.lock exists to determine install vs update
43
            $hasLockFile = File::exists(base_path('composer.lock'));
44
45
            if ($hasLockFile) {
46
                $this->info('🔄 Installing dependencies from lock file...');
47
                $this->composerInstall();
48
            } else {
49
                $this->info('🆕 Creating new lock file and installing dependencies...');
50
                $this->composerUpdate();
51
            }
52
53
            // Clear autoloader cache
54
            $this->info('🧹 Clearing autoloader cache...');
55
            $this->clearAutoloaderCache();
56
57
            $this->info('✅ Composer update completed successfully');
58
            return Command::SUCCESS;
59
60
        } catch (\Exception $e) {
61
            $this->error('❌ Composer update failed: ' . $e->getMessage());
62
            return Command::FAILURE;
63
        }
64
    }
65
66
    /**
67
     * Run composer install
68
     */
69
    private function composerInstall(): void
70
    {
71
        $command = $this->buildComposerCommand('install');
72
73
        $process = Process::timeout(600)
74
            ->path(base_path())
75
            ->run($command);
76
77
        if (!$process->successful()) {
78
            throw new \Exception('Composer install failed: ' . $process->errorOutput());
79
        }
80
81
        $this->line('  ✓ Dependencies installed successfully');
82
    }
83
84
    /**
85
     * Run composer update
86
     */
87
    private function composerUpdate(): void
88
    {
89
        $command = $this->buildComposerCommand('update');
90
91
        $process = Process::timeout(600)
92
            ->path(base_path())
93
            ->run($command);
94
95
        if (!$process->successful()) {
96
            throw new \Exception('Composer update failed: ' . $process->errorOutput());
97
        }
98
99
        $this->line('  ✓ Dependencies updated successfully');
100
    }
101
102
    /**
103
     * Build composer command with options
104
     */
105
    private function buildComposerCommand(string $action): string
106
    {
107
        $command = "composer $action";
108
109
        // Add common flags for performance
110
        $command .= ' --no-interaction --no-progress';
111
112
        if ($this->option('no-dev')) {
113
            $command .= ' --no-dev';
114
        }
115
116
        if ($this->option('prefer-dist')) {
117
            $command .= ' --prefer-dist';
118
        }
119
120
        if ($this->option('optimize') || app()->environment('production')) {
0 ignored issues
show
introduced by
The method environment() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

120
        if ($this->option('optimize') || app()->/** @scrutinizer ignore-call */ environment('production')) {
Loading history...
121
            $command .= ' --optimize-autoloader --classmap-authoritative';
122
        }
123
124
        return $command;
125
    }
126
127
    /**
128
     * Clear autoloader cache
129
     */
130
    private function clearAutoloaderCache(): void
131
    {
132
        $process = Process::timeout(30)
133
            ->path(base_path())
134
            ->run('composer dump-autoload --optimize');
135
136
        if (!$process->successful()) {
137
            $this->warn('  ⚠ Failed to optimize autoloader');
138
        } else {
139
            $this->line('  ✓ Autoloader optimized');
140
        }
141
    }
142
}
143