UserInnerLinks::addUser()   B
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 8
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 7
rs 8.8333
1
<?php
2
3
namespace kalanis\kw_user_paths;
4
5
6
use kalanis\kw_paths\ArrayPath;
7
use kalanis\kw_paths\Interfaces\IPaths;
8
use kalanis\kw_paths\PathsException;
9
10
11
/**
12
 * Class UserInnerLinks
13
 * Extend known path to user file with user's props
14
 * Limited to work with preset user from system as came from settings
15
 */
16
class UserInnerLinks
17
{
18
    protected ArrayPath $arrPath;
19
    protected UserDir $userDir;
20
    protected ?string $useUser = null;
21
    /** @var string[] */
22
    protected array $prefixPath = [];
23
24
    /**
25
     * @param string|null $useUser
26
     * @param string[] $prefixPath path to all user data, depends on storage
27
     */
28 31
    public function __construct(
29
        ?string $useUser = null,
30
        array $prefixPath = []
31
    ) {
32 31
        $this->arrPath = new ArrayPath();
33 31
        $this->userDir = new UserDir();
34 31
        $this->useUser = $useUser; // config.page.default_user
35 31
        $this->prefixPath = $prefixPath; // path where is the storage connected, can be empty
36 31
    }
37
38
    /**
39
     * Path to system things
40
     * @param string $module
41
     * @param string[] $current
42
     * @return string[]
43
     */
44 4
    public function toModulePath(string $module, array $current): array
45
    {
46 4
        return array_merge(
47 4
            $this->prefixPath,
48 4
            $this->addModuleSeparator(),
49 4
            [$module],
50
            $current
51
        );
52
    }
53
54
    /**
55
     * Path to user system things
56
     * @param string[] $current
57
     * @throws PathsException
58
     * @return string[]
59
     */
60 12
    public function toUserPath(array $current): array
61
    {
62 12
        return array_merge(
63 12
            $this->prefixPath,
64 12
            $this->addUser(false),
65
            $current
66
        );
67
    }
68
69
    /**
70
     * Path to user system things
71
     * @param string $module
72
     * @param string[] $current
73
     * @throws PathsException
74
     * @return string[]
75
     */
76 7
    public function toUserModulePath(string $module, array $current): array
77
    {
78 7
        return array_merge(
79 7
            $this->prefixPath,
80 7
            $this->addUser(false, true),
81 7
            [$module],
82
            $current
83
        );
84
    }
85
86
    /**
87
     * Path to user data
88
     * @param string[] $current
89
     * @throws PathsException
90
     * @return string[]
91
     */
92 8
    public function toFullPath(array $current): array
93
    {
94 8
        return array_merge(
95 8
            $this->prefixPath,
96 8
            $this->addUser(),
97
            $current
98
        );
99
    }
100
101
    /**
102
     * @param bool $wantDataDir
103
     * @param bool $wantModuleDir
104
     * @throws PathsException
105
     * @return string[]
106
     */
107 27
    protected function addUser(bool $wantDataDir = true, bool $wantModuleDir = false): array
108
    {
109 27
        if (is_null($this->useUser)) {
110 2
            return [];
111
        }
112 25
        $this->userDir->setUserPath($this->useUser);
113
114 25
        return array_merge(
115 25
            $this->userDir->hasHomeDir() ? $this->addPrefixSeparator() : [],
116 25
            $this->arrPath->setString(strval($this->userDir->getUserPath()))->getArray(),
117 25
            $this->userDir->hasDataDir() && $wantDataDir ? $this->addDataSeparator() : [],
118 25
            $this->userDir->hasDataDir() && $wantModuleDir ? $this->addModuleSeparator() : []
119
        );
120
    }
121
122
    /**
123
     * @return string[]
124
     */
125 5
    protected function addModuleSeparator(): array
126
    {
127 5
        return [IPaths::DIR_MODULE];
128
    }
129
130
    /**
131
     * @return string[]
132
     */
133 8
    protected function addPrefixSeparator(): array
134
    {
135 8
        return [IPaths::DIR_USER];
136
    }
137
138
    /**
139
     * @return string[]
140
     */
141 2
    protected function addDataSeparator(): array
142
    {
143 2
        return [IPaths::DIR_DATA];
144
    }
145
}
146