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

Dirs::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\Extended\Processor;
7
use kalanis\kw_files\FilesException;
8
use kalanis\kw_images\ImagesException;
9
use kalanis\kw_images\Interfaces\IIMTranslations;
10
use kalanis\kw_images\Sources;
11
use kalanis\kw_images\TLang;
12
13
14
/**
15
 * Class Dirs
16
 * Create specific content
17
 * @package kalanis\kw_images\Content
18
 */
19
class Dirs
20
{
21
    use TLang;
22
23
    /** @var ImageSize */
24
    protected $libSizes = null;
25
    /** @var Sources\Thumb */
26
    protected $libThumb = null;
27
    /** @var Sources\DirDesc */
28
    protected $libDirDesc = null;
29
    /** @var Sources\DirThumb */
30
    protected $libDirThumb = null;
31
    /** @var Processor */
32
    protected $libExt = null;
33
34 5
    public function __construct(ImageSize $sizes, Sources\Thumb $thumb, Sources\DirDesc $dirDesc, Sources\DirThumb $dirThumb, Processor $ext, ?IIMTranslations $lang = null)
35
    {
36 5
        $this->setLang($lang);
37 5
        $this->libThumb = $thumb;
38 5
        $this->libDirDesc = $dirDesc;
39 5
        $this->libDirThumb = $dirThumb;
40 5
        $this->libExt = $ext;
41 5
        $this->libSizes = $sizes;
42
    }
43
44
    /**
45
     * @param string[] $path
46
     * @throws FilesException
47
     * @return bool
48
     */
49 3
    public function exists(array $path): bool
50
    {
51 3
        return $this->libExt->dirExists($path);
52
    }
53
54
    /**
55
     * @param string[] $path
56
     * @throws FilesException
57
     * @return string
58
     */
59 1
    public function getDescription(array $path): string
60
    {
61 1
        return $this->libDirDesc->get($path);
62
    }
63
64
    /**
65
     * @param string[] $path
66
     * @param string $description
67
     * @throws FilesException
68
     * @return bool
69
     */
70 1
    public function updateDescription(array $path, string $description = ''): bool
71
    {
72 1
        if (!empty($description)) {
73 1
            return $this->libDirDesc->set($path, $description);
74
        } else {
75 1
            return $this->libDirDesc->remove($path);
76
        }
77
    }
78
79
    /**
80
     * @param string[] $path
81
     * @return string|resource
82
     */
83 1
    public function getThumb(array $path)
84
    {
85
        try {
86 1
            return $this->libDirThumb->get($path);
87 1
        } catch (FilesException $ex) {
88 1
            return '';
89
        }
90
    }
91
92
    /**
93
     * @param string[] $path
94
     * @param string $fromWhichFile
95
     * @throws FilesException
96
     * @throws ImagesException
97
     * @return bool
98
     */
99 2
    public function updateThumb(array $path, string $fromWhichFile): bool
100
    {
101 2
        return $this->libSizes->process(
102 2
            $this->libThumb->getPath(array_merge($path, [$fromWhichFile])),
103 2
            $this->libDirThumb->getPath($path)
104 2
        );
105
    }
106
107
    /**
108
     * @param string[] $path
109
     * @throws FilesException
110
     * @return bool
111
     */
112 2
    public function removeThumb(array $path): bool
113
    {
114 2
        if ($this->libDirThumb->isHere($path)) {
115 2
            if (!$this->libDirThumb->delete($path)) {
116 1
                throw new FilesException($this->getLang()->imDirThumbCannotRemoveCurrent());
117
            }
118
        }
119 1
        return true;
120
    }
121
122
    /**
123
     * @param string[] $path
124
     * @throws FilesException
125
     * @return bool
126
     */
127 2
    public function canUse(array $path): bool
128
    {
129 2
        return $this->libExt->isExtended($path);
130
    }
131
132
    /**
133
     * @param string[] $path
134
     * @throws FilesException
135
     * @return bool
136
     */
137 1
    public function create(array $path): bool
138
    {
139 1
        return $this->libExt->createDir($path, true);
140
    }
141
142
    /**
143
     * @param string[] $path
144
     * @throws FilesException
145
     * @return bool
146
     */
147 1
    public function createExtra(array $path): bool
148
    {
149 1
        return $this->libExt->makeExtended($path);
150
    }
151
}
152