Passed
Push — master ( 3dfec7...3ab830 )
by Petr
02:18
created

Images::exists()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
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
10
11
/**
12
 * Class Images
13
 * Process images parts
14
 * @package kalanis\kw_images\Content
15
 */
16
class Images
17
{
18
    /** @var ImageSize */
19
    protected $libSizes = null;
20
    /** @var Sources\Image */
21
    protected $libImage = null;
22
    /** @var Sources\Thumb */
23
    protected $libThumb = null;
24
    /** @var Sources\Desc */
25
    protected $libDesc = null;
26
27 5
    public function __construct(ImageSize $sizes, Sources\Image $image, Sources\Thumb $thumb, Sources\Desc $desc)
28
    {
29 5
        $this->libSizes = $sizes;
30 5
        $this->libImage = $image;
31 5
        $this->libThumb = $thumb;
32 5
        $this->libDesc = $desc;
33
    }
34
35
    /**
36
     * @param string[] $wantedPath
37
     * @throws FilesException
38
     * @return bool
39
     */
40 1
    public function exists(array $wantedPath): bool
41
    {
42 1
        return $this->libImage->isHere($wantedPath);
43
    }
44
45
    /**
46
     * @param string[] $wantedPath where we want to store the file
47
     * @return resource|string
48
     */
49 1
    public function get(array $wantedPath)
50
    {
51
        try {
52 1
            return $this->libImage->get($wantedPath);
53 1
        } catch (FilesException $ex) {
54 1
            return '';
55
        }
56
    }
57
58
    /**
59
     * @param string[] $wantedPath where we want to store the file
60
     * @param string $format
61
     * @throws FilesException
62
     * @return null|string
63
     */
64 1
    public function created(array $wantedPath, string $format = 'Y-m-d H:i:s'): ?string
65
    {
66 1
        return $this->libImage->getCreated($wantedPath, $format);
67
    }
68
69
    /**
70
     * @param string[] $wantedPath where we want to store the file
71
     * @param string|resource $content what we want to store as the file
72
     * @throws FilesException
73
     * @return bool
74
     */
75 1
    public function set(array $wantedPath, $content): bool
76
    {
77 1
        return $this->libImage->set($wantedPath, $content);
78
    }
79
80
    /**
81
     * @param string[] $wantedPath where we want to store the file
82
     * @throws FilesException
83
     * @return bool
84
     */
85 1
    public function remove(array $wantedPath): bool
86
    {
87 1
        $fileName = strval(array_pop($wantedPath));
88 1
        return $this->libImage->delete($wantedPath, $fileName);
89
    }
90
91
    /**
92
     * @param string[] $wantedPath
93
     * @return string[]
94
     */
95 1
    public function reversePath(array $wantedPath): array
96
    {
97 1
        return $this->libImage->getPath($wantedPath);
98
    }
99
100
    /**
101
     * @param string[] $wantedPath where we want to store the file
102
     * @return resource|string
103
     */
104 1
    public function getThumb(array $wantedPath)
105
    {
106
        try {
107 1
            return $this->libThumb->get($wantedPath);
108 1
        } catch (FilesException $ex) {
109 1
            return '';
110
        }
111
    }
112
113
    /**
114
     * @param string[] $wantedPath where we want to store the file
115
     * @throws FilesException
116
     * @throws ImagesException
117
     * @return bool
118
     */
119 2
    public function updateThumb(array $wantedPath): bool
120
    {
121 2
        return $this->libSizes->process(
122 2
            $this->libImage->getPath($wantedPath),
123 2
            $this->libThumb->getPath($wantedPath)
124 2
        );
125
    }
126
127
    /**
128
     * @param string[] $wantedPath where we want to store the file
129
     * @throws FilesException
130
     * @return bool
131
     */
132 2
    public function removeThumb(array $wantedPath): bool
133
    {
134 2
        $fileName = strval(array_pop($wantedPath));
135 2
        return $this->libThumb->delete($wantedPath, $fileName);
136
    }
137
138
    /**
139
     * @param string[] $wantedPath
140
     * @return string[]
141
     */
142 1
    public function reverseThumbPath(array $wantedPath): array
143
    {
144 1
        return $this->libThumb->getPath($wantedPath);
145
    }
146
147
    /**
148
     * @param string[] $path
149
     * @throws FilesException
150
     * @return string
151
     */
152 1
    public function getDescription(array $path): string
153
    {
154 1
        return $this->libDesc->get($path, false);
155
    }
156
157
    /**
158
     * @param string[] $wantedPath where we want to store the file
159
     * @param string $description
160
     * @throws FilesException
161
     * @return bool
162
     */
163 2
    public function updateDescription(array $wantedPath, string $description = ''): bool
164
    {
165 2
        if (!empty($description)) {
166 2
            return $this->libDesc->set($wantedPath, $description);
167
        } else {
168 1
            $fileName = strval(array_pop($wantedPath));
169 1
            return $this->libDesc->delete($wantedPath, $fileName);
170
        }
171
    }
172
173
    /**
174
     * @param string[] $wantedPath
175
     * @return string[]
176
     */
177 1
    public function reverseDescriptionPath(array $wantedPath): array
178
    {
179 1
        return $this->libDesc->getPath($wantedPath);
180
    }
181
}
182