Passed
Push — master ( 1a78b2...f5c464 )
by Petr
08:00
created

UserInnerLinks::toUserModulePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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