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

DataStorage::getMime()   A

Complexity

Conditions 2
Paths 6

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 2
rs 10
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_mime\MimeException;
9
use kalanis\kw_paths\ArrayPath;
10
use kalanis\kw_paths\PathsException;
11
use kalanis\kw_storage\Storage;
12
use kalanis\kw_storage\StorageException;
13
14
15
/**
16
 * Class DataStorage
17
 * @package kalanis\kw_mime\Check
18
 * System library to detect the mime type in storage
19
 */
20
class DataStorage implements IMimeChecks
21
{
22
    use Traits\TCheckCalls;
23
    use Traits\TResult;
24
    use Traits\TStorage;
25
26
    /** @var ArrayPath */
27
    protected $pathLib = null;
28
29 6
    public function __construct(?IMiTranslations $lang = null)
30
    {
31 6
        $this->setMiLang($lang);
32 6
        $this->pathLib = new ArrayPath();
33 6
    }
34
35 5
    public function canUse($source): bool
36
    {
37 5
        if (!$this->isMimeFunction()) {
38 1
            return false;
39
        }
40 4
        if (is_object($source) && ($source instanceof Storage)) {
41 3
            $this->setStorage($source);
42 3
            return true;
43
        }
44 1
        return false;
45
    }
46
47 3
    public function getMime(array $path): string
48
    {
49
        try {
50 3
            $this->checkMimeFunction();
51 3
            $target = $this->pathLib->setArray($path)->getString();
52 3
            $content = $this->getStorage()->get($target);
53 2
            $fileData = 'data://,' . urlencode(strval($content));
54 2
            return $this->determineResult(mime_content_type($fileData));
55 1
        } catch (StorageException | PathsException $ex) {
56 1
            throw new MimeException($ex->getMessage(), $ex->getCode(), $ex);
57
        }
58
    }
59
}
60