Passed
Push — master ( eb5b2a...d467ff )
by Petr
02:17
created

Processor::fileExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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