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