Passed
Push — master ( 3d9115...77fb6a )
by Petr
08:28
created

Images::flip()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
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\ImagesException;
8
use kalanis\kw_images\Sources;
9
use kalanis\kw_mime\MimeException;
10
use kalanis\kw_paths\PathsException;
11
12
13
/**
14
 * Class Images
15
 * Process images parts
16
 * @package kalanis\kw_images\Content
17
 */
18
class Images
19
{
20
    protected ImageSize $libSizes;
21
    protected ImageRotate $libRotate;
22
    protected Sources\Image $libImage;
23
    protected Sources\Thumb $libThumb;
24
    protected Sources\Desc $libDesc;
25
26 9
    public function __construct(ImageSize $sizes, ImageRotate $rotate, Sources\Image $image, Sources\Thumb $thumb, Sources\Desc $desc)
27
    {
28 9
        $this->libSizes = $sizes;
29 9
        $this->libRotate = $rotate;
30 9
        $this->libImage = $image;
31 9
        $this->libThumb = $thumb;
32 9
        $this->libDesc = $desc;
33 9
    }
34
35
    /**
36
     * @param string[] $wantedPath
37
     * @throws FilesException
38
     * @throws PathsException
39
     * @return bool
40
     */
41 1
    public function exists(array $wantedPath): bool
42
    {
43 1
        return $this->libImage->isHere($wantedPath);
44
    }
45
46
    /**
47
     * @param string[] $wantedPath where we want to store the file
48
     * @throws PathsException
49
     * @return resource|string
50
     */
51 1
    public function get(array $wantedPath)
52
    {
53
        try {
54 1
            return $this->libImage->get($wantedPath);
55 1
        } catch (FilesException $ex) {
56 1
            return '';
57
        }
58
    }
59
60
    /**
61
     * @param string[] $wantedPath where we want to store the file
62
     * @param string $format
63
     * @throws FilesException
64
     * @throws PathsException
65
     * @return null|string
66
     */
67 1
    public function created(array $wantedPath, string $format = 'Y-m-d H:i:s'): ?string
68
    {
69 1
        return $this->libImage->getCreated($wantedPath, $format);
70
    }
71
72
    /**
73
     * @param string[] $wantedPath where we want to store the file
74
     * @param string|resource $content what we want to store as the file
75
     * @throws FilesException
76
     * @throws PathsException
77
     * @return bool
78
     */
79 1
    public function set(array $wantedPath, $content): bool
80
    {
81 1
        return $this->libImage->set($wantedPath, $content);
82
    }
83
84
    /**
85
     * @param string[] $wantedPath where we want to store the file
86
     * @throws FilesException
87
     * @throws PathsException
88
     * @return bool
89
     */
90 1
    public function remove(array $wantedPath): bool
91
    {
92 1
        $fileName = strval(array_pop($wantedPath));
93 1
        return $this->libImage->delete($wantedPath, $fileName);
94
    }
95
96
    /**
97
     * @param string[] $wantedPath
98
     * @return string[]
99
     */
100 1
    public function reversePath(array $wantedPath): array
101
    {
102 1
        return $this->libImage->getPath($wantedPath);
103
    }
104
105
    /**
106
     * @param string[] $wantedPath where we want to store the file
107
     * @throws PathsException
108
     * @return resource|string
109
     */
110 3
    public function getThumb(array $wantedPath)
111
    {
112
        try {
113 3
            return $this->libThumb->get($wantedPath);
114 3
        } catch (FilesException $ex) {
115 3
            return '';
116
        }
117
    }
118
119
    /**
120
     * @param string[] $wantedPath where we want to store the file
121
     * @throws FilesException
122
     * @throws ImagesException
123
     * @throws MimeException
124
     * @throws PathsException
125
     * @return bool
126
     */
127 4
    public function updateThumb(array $wantedPath): bool
128
    {
129 4
        return $this->libSizes->process(
130 4
            $this->libImage->getPath($wantedPath),
131 4
            $this->libThumb->getPath($wantedPath)
132
        );
133
    }
134
135
    /**
136
     * @param string[] $wantedPath where we want to store the file
137
     * @throws FilesException
138
     * @throws PathsException
139
     * @return bool
140
     */
141 4
    public function removeThumb(array $wantedPath): bool
142
    {
143 4
        $fileName = strval(array_pop($wantedPath));
144 4
        return $this->libThumb->delete($wantedPath, $fileName);
145
    }
146
147
    /**
148
     * @param string[] $wantedPath
149
     * @return string[]
150
     */
151 1
    public function reverseThumbPath(array $wantedPath): array
152
    {
153 1
        return $this->libThumb->getPath($wantedPath);
154
    }
155
156
    /**
157
     * @param string[] $path
158
     * @throws FilesException
159
     * @throws PathsException
160
     * @return string
161
     */
162 1
    public function getDescription(array $path): string
163
    {
164 1
        return $this->libDesc->get($path, false);
165
    }
166
167
    /**
168
     * @param string[] $wantedPath where we want to store the file
169
     * @param string $description
170
     * @throws FilesException
171
     * @throws PathsException
172
     * @return bool
173
     */
174 2
    public function updateDescription(array $wantedPath, string $description = ''): bool
175
    {
176 2
        if (!empty($description)) {
177 2
            return $this->libDesc->set($wantedPath, $description);
178
        } else {
179 1
            $fileName = strval(array_pop($wantedPath));
180 1
            return $this->libDesc->delete($wantedPath, $fileName);
181
        }
182
    }
183
184
    /**
185
     * @param string[] $wantedPath
186
     * @return string[]
187
     */
188 1
    public function reverseDescriptionPath(array $wantedPath): array
189
    {
190 1
        return $this->libDesc->getPath($wantedPath);
191
    }
192
193
    /**
194
     * @param string[] $wantedPath
195
     * @param float $angle
196
     * @throws FilesException
197
     * @throws ImagesException
198
     * @throws MimeException
199
     * @throws PathsException
200
     * @return bool
201
     */
202 1
    public function rotate(array $wantedPath, float $angle): bool
203
    {
204 1
        $r1 = $r2 = $this->libRotate->process($this->libImage->getPath($wantedPath), $angle);
205 1
        if ($this->libThumb->isHere($wantedPath)) {
206 1
            $r2 = $this->updateThumb($wantedPath);
207
        }
208 1
        return $r1 && $r2;
209
    }
210
211
    /**
212
     * @param string[] $wantedPath
213
     * @param int $mode as defined by function imageflip [IMG_FLIP_HORIZONTAL, IMG_FLIP_VERTICAL, IMG_FLIP_BOTH]
214
     * @throws FilesException
215
     * @throws ImagesException
216
     * @throws MimeException
217
     * @throws PathsException
218
     * @return bool
219
     */
220 1
    public function flip(array $wantedPath, int $mode): bool
221
    {
222 1
        $r1 = $r2 = $this->libRotate->process($this->libImage->getPath($wantedPath), null, $mode);
223 1
        if ($this->libThumb->isHere($wantedPath)) {
224 1
            $r2 = $this->updateThumb($wantedPath);
225
        }
226 1
        return $r1 && $r2;
227
    }
228
}
229