Passed
Pull Request — master (#58)
by Keoghan
05:14 queued 01:03
created

Xdebug::turnOn()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
namespace App\Support\Php;
4
5
use App\Models\PhpVersion;
6
use App\PorterLibrary;
7
use Illuminate\Filesystem\Filesystem;
8
9
class Xdebug
10
{
11
    /** @var PorterLibrary */
12
    protected $porterLibrary;
13
    /** @var Filesystem */
14
    protected $filesystem;
15
16
    protected $activationSwitches = [
17
        'xdebug.remote_enable',
18
        'xdebug.default_enable',
19
    ];
20
21 4
    public function __construct(PorterLibrary $porterLibrary, Filesystem $filesystem)
22
    {
23 4
        $this->porterLibrary = $porterLibrary;
24 4
        $this->filesystem = $filesystem;
25 4
    }
26
27
    /**
28
     * Turn Xdebug on for all PHP versions, on the one passed.
29
     * We are just editing the config, rather than disabling wholesale.
30
     *
31
     * @param PhpVersion|null $phpVersion
32
     *
33
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
34
     */
35 2
    public function turnOn(PhpVersion $phpVersion = null)
36
    {
37 2
        $phpVersions = $phpVersion ? collect([$phpVersion]) : PhpVersion::all();
38 2
        $config = $this->porterLibrary->configPath().'/';
39
40 2
        foreach ($phpVersions as $phpVersion) {
41 2
            $this->enable($config.$phpVersion->cli_name);
42 2
            $this->enable($config.$phpVersion->fpm_name);
43
        }
44 2
    }
45
46
    /**
47
     * Turn Xdebug off for all PHP versions, on the one passed.
48
     * We are just editing the config, rather than disabling wholesale.
49
     *
50
     * @param PhpVersion|null $phpVersion
51
     */
52 2
    public function turnOff(PhpVersion $phpVersion = null)
53
    {
54 2
        $phpVersions = $phpVersion ? collect([$phpVersion]) : PhpVersion::all();
55 2
        $config = $this->porterLibrary->configPath().'/';
56
57 2
        foreach ($phpVersions as $phpVersion) {
58 2
            $this->disable($config.$phpVersion->cli_name);
59 2
            $this->disable($config.$phpVersion->fpm_name);
60
        }
61 2
    }
62
63
    /**
64
     * Edit the xdebug.ini file to enable the xdebug.
65
     *
66
     * @param string $path
67
     *
68
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
69
     */
70 2
    protected function enable(string $path)
71
    {
72 2
        $file = $path.'/xdebug.ini';
73
74 2
        $contents = $this->filesystem->get($file);
75
76 2
        foreach ($this->activationSwitches as $key) {
77 2
            $contents = str_replace($key.'=0', $key.'=1', $contents);
78
        }
79
80 2
        $this->filesystem->put($file, $contents);
81 2
    }
82
83
    /**
84
     * Edit the xdebug.ini file to disable xdebug.
85
     *
86
     * @param string $path
87
     *
88
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
89
     */
90 2
    protected function disable(string $path)
91
    {
92 2
        $file = $path.'/xdebug.ini';
93
94 2
        $contents = $this->filesystem->get($file);
95
96 2
        foreach ($this->activationSwitches as $key) {
97 2
            $contents = str_replace($key.'=1', $key.'=0', $contents);
98
        }
99
100 2
        $this->filesystem->put($file, $contents);
101 2
    }
102
}
103