Passed
Push — dbal ( 396fa2...d36e46 )
by Greg
11:43
created

FilesystemFactory::media()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2022 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 <https://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 League\Flysystem\Filesystem;
27
use League\Flysystem\FilesystemOperator;
28
use League\Flysystem\Local\LocalFilesystemAdapter;
29
30
use function realpath;
31
32
/**
33
 * Make a filesystem.
34
 */
35
class FilesystemFactory implements FilesystemFactoryInterface
36
{
37
    private const ROOT_DIR = __DIR__ . '/../..';
38
39
    /**
40
     * Create a filesystem for the user's data folder.
41
     *
42
     * @return FilesystemOperator
43
     */
44
    public function data(): FilesystemOperator
45
    {
46
        $data_dir = Site::getPreference('INDEX_DIRECTORY');
47
48
        return new Filesystem(new LocalFilesystemAdapter($data_dir));
49
    }
50
51
    /**
52
     * Describe a filesystem for the user's data folder.
53
     *
54
     * @return string
55
     */
56
    public function dataName(): string
57
    {
58
        return Site::getPreference('INDEX_DIRECTORY');
59
    }
60
61
    /**
62
     * Create a filesystem for a tree's media folder.
63
     *
64
     * @param Tree $tree
65
     *
66
     * @return FilesystemOperator
67
     */
68
    public function media(Tree $tree): FilesystemOperator
69
    {
70
        $media_dir = $tree->getPreference('MEDIA_DIRECTORY');
71
        $adapter   = new ChrootAdapter($this->data(), $media_dir);
72
73
        return new Filesystem($adapter);
74
    }
75
76
    /**
77
     * Create a filesystem for the application's root folder.
78
     *
79
     * @return FilesystemOperator
80
     */
81
    public function root(): FilesystemOperator
82
    {
83
        return new Filesystem(new LocalFilesystemAdapter(self::ROOT_DIR));
84
    }
85
86
    /**
87
     * Describe a filesystem for the application's root folder.
88
     *
89
     * @return string
90
     */
91
    public function rootName(): string
92
    {
93
        return realpath(self::ROOT_DIR) . '/';
94
    }
95
}
96