Failed Conditions
Pull Request — master (#95)
by Keoghan
05:44
created

XDebug::disable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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
        foreach (PhpVersion::active()->get() as $version) {
29 1
            if (! $this->enable($version)) {
30
                // If enable fails it's because it was already enabled
31 1
                continue;
32
            }
33
34
            $this->porter->softRestart($version->getFpmNameAttribute());
35
        }
36 4
    }
37
38
    public function turnOff()
39
    {
40
        Setting::updateOrCreate('use_xdebug', 'off');
41
42
        foreach (PhpVersion::active()->get() as $version) {
43
            if (! $this->disable($version)) {
44
                // If disable fails it's because it was already disabled
45
                continue;
46
            }
47
48
            $this->porter->softRestart($version->getFpmNameAttribute());
49
        }
50
    }
51
52
    /**
53
     * Move the ini file to .ini to enable
54
     *
55
     * @param  PhpVersion  $version
56
     *
57
     * @return bool
58
     */
59 1
    protected function enable(PhpVersion $version)
60
    {
61 1
        return $this->moveIniFile($version, 'xdebug.bak', 'xdebug.ini');
62
    }
63
64
    /**
65
     * Move the ini file to .bak to disable
66
     *
67
     * @param  PhpVersion  $version
68
     *
69
     * @return bool
70
     */
71
    protected function disable(PhpVersion $version)
72
    {
73
        return $this->moveIniFile($version, 'xdebug.ini', 'xdebug.bak');
74
    }
75
76
    /**
77
     * Move the ini file
78
     *
79
     * If there was any output captured it's because it failed - the file wasn't able to be moved
80
     * Most likely because it was moved before
81
     *
82
     * @param  PhpVersion  $version
83
     * @param $from
84
     * @param $to
85
     *
86
     * @return bool
87
     */
88 1
    protected function moveIniFile(PhpVersion $version, $from, $to)
89
    {
90 1
        $move = "mv /etc/php/{$version->version_number}/mods-available/{$from} /etc/php/{$version->version_number}/mods-available/{$to}";
91
92 1
        ob_start();
93 1
        $this->dockerCompose->execContainer($version->getFpmNameAttribute())->append($move)->interactive()->perform();
94 1
        if (ob_get_clean()) {
95 1
            return false;
96
        }
97
98
        return true;
99
    }
100
}
101