FilesystemManager::setZonePath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 2
b 0
f 1
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
/*
4
 * This file is part of PHP DNS Server.
5
 *
6
 * (c) Yif Swery <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace yswery\DNS\Filesystem;
13
14
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
15
use Symfony\Component\Filesystem\Filesystem;
16
use yswery\DNS\Resolver\JsonFileSystemResolver;
17
18
class FilesystemManager
19
{
20
    /**
21
     * @var Filesystem
22
     */
23
    protected $filesystem;
24
25
    /**
26
     * @var string
27
     */
28
    protected $basePath;
29
30
    /**
31
     * @var string
32
     */
33
    protected $zonePath;
34
35
    public function __construct($basePath = null, $zonePath = null)
36
    {
37
        if ($basePath) {
38
            $this->setBasePath($basePath);
39
        }
40
41
        if ($zonePath) {
42
            $this->setZonePath($zonePath);
43
        }
44
45
        $this->registerFilesystem();
46
    }
47
48
    protected function registerFilesystem()
49
    {
50
        $this->filesystem = new Filesystem();
51
52
        // make sure our directories exist
53
        if (!$this->filesystem->exists($this->zonePath())) {
54
            try {
55
                $this->filesystem->mkdir($this->zonePath(), 0700);
56
            } catch (IOExceptionInterface $e) {
57
                // todo: implement logging functions
58
            }
59
        }
60
    }
61
62
    public function setBasePath($basePath)
63
    {
64
        $this->basePath = rtrim($basePath, '\/');
65
66
        return $this;
67
    }
68
69
    public function basePath()
70
    {
71
        return $this->basePath;
72
    }
73
74
    public function setZonePath($zonePath)
75
    {
76
        $this->zonePath = rtrim($zonePath, '\/');
77
78
        return $this;
79
    }
80
81
    public function zonePath()
82
    {
83
        return $this->zonePath ?: $this->basePath.DIRECTORY_SEPARATOR.'zones';
84
    }
85
86
    /**
87
     * @param string $zone
88
     *
89
     * @return JsonFileSystemResolver
90
     *
91
     * @throws \yswery\DNS\UnsupportedTypeException
92
     */
93
    public function getZone(string $zone)
94
    {
95
        $zoneFile = $this->basePath().DIRECTORY_SEPARATOR.$zone.'.json';
96
97
        return new JsonFileSystemResolver($zoneFile);
0 ignored issues
show
Bug introduced by
$zoneFile of type string is incompatible with the type yswery\DNS\Filesystem\FilesystemManager expected by parameter $filesystemManager of yswery\DNS\Resolver\Json...Resolver::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        return new JsonFileSystemResolver(/** @scrutinizer ignore-type */ $zoneFile);
Loading history...
98
    }
99
}
100