Passed
Push — master ( b263ca...3dfec7 )
by Petr
02:26
created

Images   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 126
rs 10
ccs 35
cts 35
cp 1
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 6 2
A __construct() 0 6 1
A set() 0 3 1
A updateThumb() 0 5 1
A removeThumb() 0 4 1
A created() 0 3 1
A getThumb() 0 6 2
A remove() 0 4 1
A getDescription() 0 3 1
A updateDescription() 0 7 2
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 where we want to store the file
37
     * @return resource|string
38
     */
39 1
    public function get(array $wantedPath)
40
    {
41
        try {
42 1
            return $this->libImage->get($wantedPath);
43 1
        } catch (FilesException $ex) {
44 1
            return '';
45
        }
46
    }
47
48
    /**
49
     * @param string[] $wantedPath where we want to store the file
50
     * @param string $format
51
     * @throws FilesException
52
     * @return null|string
53
     */
54 1
    public function created(array $wantedPath, string $format = 'Y-m-d H:i:s'): ?string
55
    {
56 1
        return $this->libImage->getCreated($wantedPath, $format);
57
    }
58
59
    /**
60
     * @param string[] $wantedPath where we want to store the file
61
     * @param string|resource $content what we want to store as the file
62
     * @throws FilesException
63
     * @return bool
64
     */
65 1
    public function set(array $wantedPath, $content): bool
66
    {
67 1
        return $this->libImage->set($wantedPath, $content);
68
    }
69
70
    /**
71
     * @param string[] $wantedPath where we want to store the file
72
     * @throws FilesException
73
     * @return bool
74
     */
75 1
    public function remove(array $wantedPath): bool
76
    {
77 1
        $fileName = strval(array_pop($wantedPath));
78 1
        return $this->libImage->delete($wantedPath, $fileName);
79
    }
80
81
    /**
82
     * @param string[] $wantedPath where we want to store the file
83
     * @return resource|string
84
     */
85 1
    public function getThumb(array $wantedPath)
86
    {
87
        try {
88 1
            return $this->libThumb->get($wantedPath);
89 1
        } catch (FilesException $ex) {
90 1
            return '';
91
        }
92
    }
93
94
    /**
95
     * @param string[] $wantedPath where we want to store the file
96
     * @throws FilesException
97
     * @throws ImagesException
98
     * @return bool
99
     */
100 2
    public function updateThumb(array $wantedPath): bool
101
    {
102 2
        return $this->libSizes->process(
103 2
            $this->libImage->getPath($wantedPath),
104 2
            $this->libThumb->getPath($wantedPath)
105 2
        );
106
    }
107
108
    /**
109
     * @param string[] $wantedPath where we want to store the file
110
     * @throws FilesException
111
     * @return bool
112
     */
113 2
    public function removeThumb(array $wantedPath): bool
114
    {
115 2
        $fileName = strval(array_pop($wantedPath));
116 2
        return $this->libThumb->delete($wantedPath, $fileName);
117
    }
118
119
    /**
120
     * @param string[] $path
121
     * @throws FilesException
122
     * @return string
123
     */
124 1
    public function getDescription(array $path): string
125
    {
126 1
        return $this->libDesc->get($path, false);
127
    }
128
129
    /**
130
     * @param string[] $wantedPath where we want to store the file
131
     * @param string $description
132
     * @throws FilesException
133
     * @return bool
134
     */
135 2
    public function updateDescription(array $wantedPath, string $description = ''): bool
136
    {
137 2
        if (!empty($description)) {
138 2
            return $this->libDesc->set($wantedPath, $description);
139
        } else {
140 1
            $fileName = strval(array_pop($wantedPath));
141 1
            return $this->libDesc->delete($wantedPath, $fileName);
142
        }
143
    }
144
}
145