Passed
Push — master ( 2ddebf...84c74c )
by Petr
09:17 queued 01:35
created

ALocalVolume::canUse()   A

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
    /** @var string */
23
    protected $startPath = '';
24
    /** @var ArrayPath */
25
    protected $pathLib = null;
26
27 12
    public function __construct(?IMiTranslations $lang = null)
28
    {
29 12
        $this->setMiLang($lang);
30 12
        $this->pathLib = new ArrayPath();
31 12
    }
32
33 10
    public function canUse($source): bool
34
    {
35 10
        if (!$this->hasDependencies()) {
36 1
            return false;
37
        }
38 9
        if (!is_string($source)) {
39 3
            return false;
40
        }
41 7
        $path = realpath($source);
42 7
        if (false === $path) {
43 3
            return false;
44
        }
45 4
        $this->startPath = $path;
46 4
        return true;
47
    }
48
49
    abstract protected function hasDependencies(): bool;
50
51
    /**
52
     * @param string[] $path
53
     * @throws PathsException
54
     * @return string
55
     */
56 6
    protected function pathOnVolume(array $path): string
57
    {
58 6
        return $this->startPath . DIRECTORY_SEPARATOR . $this->pathLib->setArray($path)->getString();
59
    }
60
}
61