Test Failed
Pull Request — master (#95)
by Keoghan
04:44
created

XDebug   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 55.17%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 27
c 2
b 0
f 0
dl 0
loc 59
ccs 16
cts 29
cp 0.5517
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A turnOn() 0 21 4
A __construct() 0 4 1
A turnOff() 0 21 4
1
<?php
2
3
namespace App\Support\XDebug;
4
5
use App\Models\PhpVersion;
6
use App\Models\Setting;
7
use App\Porter;
8
use Illuminate\Support\Str;
9
use App\Support\Console\DockerCompose\CliCommandFactory;
10
11
class XDebug
12
{
13
    /** @var Porter */
14
    protected $porter;
15
16
    /** @var CliCommandFactory */
17
    protected $dockerCompose;
18
19 193
    public function __construct(Porter $porter, CliCommandFactory $dockerCompose)
20
    {
21 193
        $this->porter = $porter;
22 193
        $this->dockerCompose = $dockerCompose;
23 193
    }
24
25 4
    public function turnOn()
26
    {
27 4
        Setting::updateOrCreate('use_xdebug', 'on');
28
29 4
        $results = [];
30 4
        foreach (PhpVersion::active()->get() as $version) {
31 1
            $move = "mv /etc/php/{$version->version_number}/mods-available/xdebug.bak /etc/php/{$version->version_number}/mods-available/xdebug.ini";
32
33 1
            ob_start();
34 1
            $this->dockerCompose->execContainer($version->getFpmNameAttribute())->append($move)->interactive()->perform();
35 1
            $results[$version->id] = ob_get_clean();
36
        }
37
38 4
        foreach (PhpVersion::active()->get() as $version) {
39 1
            if ($results[$version->id]) {
40
                // If there was any output captured it's because it failed - the file wasn't able to be moved
41
                // Most likely because it was moved before
42 1
                continue;
43
            }
44
45
            $this->porter->softRestart($version->getFpmNameAttribute());
46
        }
47 4
    }
48
49
    public function turnOff()
50
    {
51
        Setting::updateOrCreate('use_xdebug', 'off');
52
53
        $results = [];
54
        foreach (PhpVersion::active()->get() as $version) {
55
            $move = "mv /etc/php/{$version->version_number}/mods-available/xdebug.ini /etc/php/{$version->version_number}/mods-available/xdebug.bak";
56
57
            ob_start();
58
            $this->dockerCompose->execContainer($version->getFpmNameAttribute())->append($move)->interactive()->perform();
59
            $results[$version->id] = ob_get_clean();
60
        }
61
62
        foreach (PhpVersion::active()->get() as $version) {
63
            if ($results[$version->id]) {
64
                // If there was any output captured it's because it failed - the file wasn't able to be moved
65
                // Most likely because it was moved before
66
                continue;
67
            }
68
69
            $this->porter->softRestart($version->getFpmNameAttribute());
70
        }
71
    }
72
}
73