Test Failed
Branch master (17185c)
by Keoghan
06:01 queued 02:00
created

Config::getConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 2
cts 4
cp 0.5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.5
1
<?php
2
3
namespace App\Support\Dnsmasq;
4
5
use App\PorterLibrary;
6
use Illuminate\Contracts\Filesystem\FileNotFoundException;
7
use Illuminate\Filesystem\Filesystem;
8
9
class Config
10
{
11
    /** @var Filesystem */
12
    protected $files;
13
14
    /** @var PorterLibrary */
15
    private $porterLibrary;
16
17 4
    public function __construct(Filesystem $files, PorterLibrary $porterLibrary)
18
    {
19 4
        $this->files = $files;
20 4
        $this->porterLibrary = $porterLibrary;
21 4
    }
22
23 1
    public function updateDomain($from, $to)
24
    {
25 1
        $newConfig = preg_replace("/\/.{$from}\//", "/.{$to}/", $this->getConfig());
26
27 1
        $this->putConfig($newConfig);
28 1
    }
29
30 1
    public function updateIp($to)
31
    {
32 1
        $pattern = "/\/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/";
33 1
        $newConfig = preg_replace($pattern, "/{$to}", $this->getConfig());
34
35 1
        $this->putConfig($newConfig);
36 1
    }
37
38 2
    protected function getPath()
39
    {
40 2
        return $this->porterLibrary->configPath().'/dnsmasq/dnsmasq.conf';
41
    }
42
43 2
    protected function getConfig()
44
    {
45
        try {
46 2
            return $this->files->get($this->getPath());
47
        } catch (FileNotFoundException $e) {
48
            return '';
49
        }
50
    }
51
52 2
    protected function putConfig($content)
53
    {
54 2
        $this->files->put($this->getPath(), $content);
55 2
    }
56
}
57