ALocalVolume::canUse()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
1
<?php
2
3
namespace kalanis\kw_mime\Check;
4
5
6
use kalanis\kw_mime\Interfaces\IMimeChecks;
7
use kalanis\kw_mime\Interfaces\IMiTranslations;
8
use kalanis\kw_paths\ArrayPath;
9
use kalanis\kw_paths\PathsException;
10
11
12
/**
13
 * Class ALocalVolume
14
 * @package kalanis\kw_mime\Check
15
 * System libraries to get the mime type - works over local volume, not on remote files
16
 */
17
abstract class ALocalVolume implements IMimeChecks
18
{
19
    use Traits\TCheckCalls;
20
    use Traits\TResult;
21
22
    protected string $startPath = '';
23
    protected ArrayPath $pathLib;
24
25 12
    public function __construct(?IMiTranslations $lang = null)
26
    {
27 12
        $this->setMiLang($lang);
28 12
        $this->pathLib = new ArrayPath();
29
    }
30
31 10
    public function canUse($source): bool
32
    {
33 10
        if (!$this->hasDependencies()) {
34 1
            return false;
35
        }
36 9
        if (!is_string($source)) {
37 3
            return false;
38
        }
39 7
        $path = realpath($source);
40 7
        if (false === $path) {
41 3
            return false;
42
        }
43 4
        $this->startPath = $path;
44 4
        return true;
45
    }
46
47
    abstract protected function hasDependencies(): bool;
48
49
    /**
50
     * @param string[] $path
51
     * @throws PathsException
52
     * @return string
53
     */
54 6
    protected function pathOnVolume(array $path): string
55
    {
56 6
        return $this->startPath . DIRECTORY_SEPARATOR . $this->pathLib->setArray($path)->getString();
57
    }
58
}
59