BasicOperations::copy()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
nc 7
nop 3
dl 0
loc 24
ccs 16
cts 16
cp 1
crap 5
rs 9.3888
c 1
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_images\Content;
4
5
6
use kalanis\kw_files\FilesException;
7
use kalanis\kw_images\Sources;
8
use kalanis\kw_paths\PathsException;
9
10
11
/**
12
 * Class BasicOperations
13
 * Operations over files
14
 * @package kalanis\kw_images\Content
15
 */
16
class BasicOperations
17
{
18
    protected Sources\Image $libImage;
19
    protected Sources\Thumb $libThumb;
20
    protected Sources\Desc $libDesc;
21
22 11
    public function __construct(Sources\Image $image, Sources\Thumb $thumb, Sources\Desc $desc)
23
    {
24 11
        $this->libImage = $image;
25 11
        $this->libThumb = $thumb;
26 11
        $this->libDesc = $desc;
27
    }
28
29
    /**
30
     * @param string[] $currentPath
31
     * @param string[] $targetDir
32
     * @param bool $overwrite
33
     * @throws FilesException
34
     * @throws PathsException
35
     * @return bool
36
     */
37 3
    public function copy(array $currentPath, array $targetDir, bool $overwrite = false): bool
38
    {
39 3
        $fullPath = array_values($currentPath);
40 3
        $fileName = strval(array_pop($currentPath));
41
42 3
        $this->libImage->copy($fileName, $currentPath, $targetDir, $overwrite);
43 3
        if ($this->libThumb->isHere($fullPath)) {
44
            try {
45 3
                $this->libThumb->copy($fileName, $currentPath, $targetDir, $overwrite);
46 1
            } catch (FilesException $ex) {
47 1
                $this->libImage->delete($targetDir, $fileName);
48 1
                throw $ex;
49
            }
50
        }
51 2
        if ($this->libDesc->isHere($fullPath)) {
52
            try {
53 2
                $this->libDesc->copy($fileName, $currentPath, $targetDir, $overwrite);
54 1
            } catch (FilesException $ex) {
55 1
                $this->libThumb->delete($targetDir, $fileName);
56 1
                $this->libImage->delete($targetDir, $fileName);
57 1
                throw $ex;
58
            }
59
        }
60 1
        return true;
61
    }
62
63
    /**
64
     * @param string[] $currentPath
65
     * @param string[] $targetDir
66
     * @param bool $overwrite
67
     * @throws FilesException
68
     * @throws PathsException
69
     * @return bool
70
     */
71 3
    public function move(array $currentPath, array $targetDir, bool $overwrite = false): bool
72
    {
73 3
        $fullPath = array_values($currentPath);
74 3
        $fileName = strval(array_pop($currentPath));
75
76 3
        $this->libImage->move($fileName, $currentPath, $targetDir, $overwrite);
77 3
        if ($this->libThumb->isHere($fullPath)) {
78
            try {
79 3
                $this->libThumb->move($fileName, $currentPath, $targetDir, $overwrite);
80 1
            } catch (FilesException $ex) {
81 1
                $this->libImage->move($fileName, $targetDir, $currentPath);
82 1
                throw $ex;
83
            }
84
        }
85 2
        if ($this->libDesc->isHere($fullPath)) {
86
            try {
87 2
                $this->libDesc->move($fileName, $currentPath, $targetDir, $overwrite);
88 1
            } catch (FilesException $ex) {
89 1
                $this->libThumb->move($fileName, $targetDir, $currentPath);
90 1
                $this->libImage->move($fileName, $targetDir, $currentPath);
91 1
                throw $ex;
92
            }
93
        }
94 1
        return true;
95
    }
96
97
    /**
98
     * @param string[] $currentPath
99
     * @param string $targetName
100
     * @param bool $overwrite
101
     * @throws FilesException
102
     * @throws PathsException
103
     * @return bool
104
     */
105 3
    public function rename(array $currentPath, string $targetName, bool $overwrite = false): bool
106
    {
107 3
        $fullPath = array_values($currentPath);
108 3
        $fileName = strval(array_pop($currentPath));
109
110 3
        $this->libImage->rename($currentPath, $fileName, $targetName, $overwrite);
111 3
        if ($this->libThumb->isHere($fullPath)) {
112
            try {
113 3
                $this->libThumb->rename($currentPath, $fileName, $targetName, $overwrite);
114 1
            } catch (FilesException $ex) {
115 1
                $this->libImage->rename($currentPath, $targetName, $fileName);
116 1
                throw $ex;
117
            }
118
        }
119 2
        if ($this->libDesc->isHere($fullPath)) {
120
            try {
121 2
                $this->libDesc->rename($currentPath, $fileName, $targetName, $overwrite);
122 1
            } catch (FilesException $ex) {
123 1
                $this->libThumb->rename($currentPath, $targetName, $fileName);
124 1
                $this->libImage->rename($currentPath, $targetName, $fileName);
125 1
                throw $ex;
126
            }
127
        }
128 1
        return true;
129
    }
130
131
    /**
132
     * @param string[] $path
133
     * @throws FilesException
134
     * @throws PathsException
135
     * @return bool
136
     */
137 1
    public function delete(array $path): bool
138
    {
139 1
        $fileName = strval(array_pop($path));
140
141 1
        $r1 = $this->libDesc->delete($path, $fileName);
142 1
        $r2 = $this->libThumb->delete($path, $fileName);
143 1
        $r3 = $this->libImage->delete($path, $fileName);
144 1
        return $r1 && $r2 && $r3;
145
    }
146
}
147