Passed
Push — master ( 933866...696755 )
by Greg
06:17
created

FilesystemFactory::rootName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2020 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Factories;
21
22
use Fisharebest\Flysystem\Adapter\ChrootAdapter;
23
use Fisharebest\Webtrees\Contracts\FilesystemFactoryInterface;
24
use Fisharebest\Webtrees\Site;
25
use Fisharebest\Webtrees\Tree;
26
use Fisharebest\Webtrees\Webtrees;
27
use League\Flysystem\Adapter\Local;
28
use League\Flysystem\Cached\CachedAdapter;
29
use League\Flysystem\Cached\Storage\Memory;
30
use League\Flysystem\Filesystem;
31
use League\Flysystem\FilesystemInterface;
32
33
use function realpath;
34
35
/**
36
 * Make a filesyste,.
37
 */
38
class FilesystemFactory implements FilesystemFactoryInterface
39
{
40
    private const ROOT_DIR = __DIR__ . '/../../..';
41
42
    /**
43
     * Create a filesystem for the user's data folder.
44
     *
45
     * @return FilesystemInterface
46
     */
47
    public function data(): FilesystemInterface
48
    {
49
        $data_dir = Site::getPreference('INDEX_DIRECTORY', Webtrees::DATA_DIR);
50
51
        return new Filesystem(new CachedAdapter(new Local($data_dir), new Memory()));
52
    }
53
54
    /**
55
     * Describe a filesystem for the user's data folder.
56
     *
57
     * @return string
58
     */
59
    public function dataName(): string
60
    {
61
        return Site::getPreference('INDEX_DIRECTORY', Webtrees::DATA_DIR);
62
    }
63
64
    /**
65
     * Create a filesystem for a tree's media folder.
66
     *
67
     * @param Tree $tree
68
     *
69
     * @return FilesystemInterface
70
     */
71
    public function media(Tree $tree): FilesystemInterface
72
    {
73
        $media_dir = $tree->getPreference('MEDIA_DIRECTORY', 'media/');
74
        $adapter   = new ChrootAdapter($this->data(), $media_dir);
75
76
        return new Filesystem($adapter);
77
    }
78
79
    /**
80
     * Create a filesystem for the application's root folder.
81
     *
82
     * @return FilesystemInterface
83
     */
84
    public function root(): FilesystemInterface
85
    {
86
        return new Filesystem(new CachedAdapter(new Local(self::ROOT_DIR), new Memory()));
87
    }
88
89
    /**
90
     * Describe a filesystem for the application's root folder.
91
     *
92
     * @return string
93
     */
94
    public function rootName(): string
95
    {
96
        return realpath(self::ROOT_DIR) . '/';
97
    }
98
}
99