Processor::removeExtended()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 20
ccs 15
cts 15
cp 1
crap 7
rs 8.8333
1
<?php
2
3
namespace kalanis\kw_files\Extended;
4
5
6
use kalanis\kw_files\Access\CompositeAdapter;
7
use kalanis\kw_files\FilesException;
8
use kalanis\kw_paths\PathsException;
9
10
11
/**
12
 * Class Processor
13
 * low-level work with extended dirs - which contains other params than just files and sub dirs
14
 */
15
class Processor
16
{
17
    protected CompositeAdapter $files;
18
    protected Config $config;
19
20 5
    public function __construct(CompositeAdapter $files, Config $config)
21
    {
22 5
        $this->files = $files;
23 5
        $this->config = $config;
24
    }
25
26
    /**
27
     * @param string[] $path the path inside the web root dir
28
     * @throws FilesException
29
     * @throws PathsException
30
     * @return bool
31
     */
32 1
    public function exists(array $path): bool
33
    {
34 1
        return $this->files->exists($path);
35
    }
36
37
    /**
38
     * @param string[] $path the path inside the web root dir
39
     * @throws FilesException
40
     * @throws PathsException
41
     * @return bool
42
     */
43 1
    public function dirExists(array $path): bool
44
    {
45 1
        return $this->files->isDir($path);
46
    }
47
48
    /**
49
     * @param string[] $path the path inside the web root dir
50
     * @throws FilesException
51
     * @throws PathsException
52
     * @return bool
53
     */
54 1
    public function fileExists(array $path): bool
55
    {
56 1
        return $this->files->isFile($path);
57
    }
58
59
    /**
60
     * @param string[] $path the path inside the web root dir
61
     * @param bool $makeExtra
62
     * @throws FilesException
63
     * @throws PathsException
64
     * @return bool
65
     */
66 5
    public function createDir(array $path, bool $makeExtra = false): bool
67
    {
68 5
        return $this->files->createDir($path, true)
69 5
            && ($makeExtra ? $this->makeExtended($path) : true);
70
    }
71
72
    /**
73
     * @param string[] $path the path inside the web root dir
74
     * @throws FilesException
75
     * @throws PathsException
76
     * @return bool
77
     */
78 1
    public function removeDir(array $path): bool
79
    {
80 1
        return ($this->isExtended($path) ? $this->removeExtended($path) : true) && $this->files->deleteDir($path);
81
    }
82
83
    /**
84
     * @param string[] $path
85
     * @throws FilesException
86
     * @throws PathsException
87
     * @return bool
88
     */
89 1
    public function isExtended(array $path): bool
90
    {
91 1
        $desc = array_merge($path, [$this->config->getDescDir()]);
92 1
        $thumb = array_merge($path, [$this->config->getThumbDir()]);
93 1
        return $this->files->exists($desc)
94 1
            && $this->files->isDir($desc)
95 1
            && $this->files->exists($thumb)
96 1
            && $this->files->isDir($thumb)
97 1
        ;
98
    }
99
100
    /**
101
     * Make dir with extended properties
102
     * @param string[] $path
103
     * @throws FilesException
104
     * @throws PathsException
105
     * @return bool
106
     */
107 3
    public function makeExtended(array $path): bool
108
    {
109 3
        $desc = array_merge($path, [$this->config->getDescDir()]);
110 3
        $thumb = array_merge($path, [$this->config->getThumbDir()]);
111 3
        $descExists = $this->files->exists($desc);
112 3
        $thumbExists = $this->files->exists($thumb);
113 3
        if ($descExists && !$this->files->isDir($desc)) {
114 1
            return false;
115
        }
116 2
        if ($thumbExists && !$this->files->isDir($thumb)) {
117 1
            return false;
118
        }
119 1
        $ret = true;
120 1
        if (!$descExists) {
121 1
            $ret &= $this->files->createDir($desc, true);
122
        }
123 1
        if (!$thumbExists) {
124 1
            $ret &= $this->files->createDir($thumb, true);
125
        }
126 1
        return boolval($ret);
127
    }
128
129
    /**
130
     * @param string[] $path
131
     * @throws FilesException
132
     * @throws PathsException
133
     * @return bool
134
     */
135 3
    public function removeExtended(array $path): bool
136
    {
137 3
        $desc = array_merge($path, [$this->config->getDescDir()]);
138 3
        $thumb = array_merge($path, [$this->config->getThumbDir()]);
139 3
        $descExists = $this->files->exists($desc);
140 3
        $thumbExists = $this->files->exists($thumb);
141 3
        if ($descExists && !$this->files->isDir($desc)) {
142 1
            return false;
143
        }
144 2
        if ($thumbExists && !$this->files->isDir($thumb)) {
145 1
            return false;
146
        }
147 1
        $ret = true;
148 1
        if ($descExists) {
149 1
            $ret &= $this->files->deleteDir($desc, true);
150
        }
151 1
        if ($thumbExists) {
152 1
            $ret &= $this->files->deleteDir($thumb, true);
153
        }
154 1
        return boolval($ret);
155
    }
156
}
157