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

Dirs::canUse()   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 string
48
     */
49 1
    public function getDescription(array $path): string
50
    {
51 1
        return $this->libDirDesc->get($path);
52
    }
53
54
    /**
55
     * @param string[] $path
56
     * @param string $description
57
     * @throws FilesException
58
     * @return bool
59
     */
60 1
    public function updateDescription(array $path, string $description = ''): bool
61
    {
62 1
        if (!empty($description)) {
63 1
            return $this->libDirDesc->set($path, $description);
64
        } else {
65 1
            return $this->libDirDesc->remove($path);
66
        }
67
    }
68
69
    /**
70
     * @param string[] $path
71
     * @return string|resource
72
     */
73 1
    public function getThumb(array $path)
74
    {
75
        try {
76 1
            return $this->libDirThumb->get($path);
77 1
        } catch (FilesException $ex) {
78 1
            return '';
79
        }
80
    }
81
82
    /**
83
     * @param string[] $path
84
     * @param string $fromWhichFile
85
     * @throws FilesException
86
     * @throws ImagesException
87
     * @return bool
88
     */
89 2
    public function updateThumb(array $path, string $fromWhichFile): bool
90
    {
91 2
        return $this->libSizes->process(
92 2
            $this->libThumb->getPath(array_merge($path, [$fromWhichFile])),
93 2
            $this->libDirThumb->getPath($path)
94 2
        );
95
    }
96
97
    /**
98
     * @param string[] $path
99
     * @throws FilesException
100
     * @return bool
101
     */
102 2
    public function removeThumb(array $path): bool
103
    {
104 2
        if ($this->libDirThumb->isHere($path)) {
105 2
            if (!$this->libDirThumb->delete($path)) {
106 1
                throw new FilesException($this->getLang()->imDirThumbCannotRemoveCurrent());
107
            }
108
        }
109 1
        return true;
110
    }
111
112
    /**
113
     * @param string[] $path
114
     * @throws FilesException
115
     * @return bool
116
     */
117 2
    public function canUse(array $path): bool
118
    {
119 2
        return $this->libExt->isExtended($path);
120
    }
121
122
    /**
123
     * @param string[] $path
124
     * @throws FilesException
125
     * @return bool
126
     */
127 1
    public function create(array $path): bool
128
    {
129 1
        return $this->libExt->createDir($path, true);
130
    }
131
132
    /**
133
     * @param string[] $path
134
     * @throws FilesException
135
     * @return bool
136
     */
137 1
    public function createExtra(array $path): bool
138
    {
139 1
        return $this->libExt->makeExtended($path);
140
    }
141
}
142