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

DataFiles   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 37
ccs 19
cts 19
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMime() 0 9 2
A __construct() 0 5 1
A canUse() 0 10 4
1
<?php
2
3
namespace kalanis\kw_mime\Check;
4
5
6
use kalanis\kw_files\FilesException;
7
use kalanis\kw_files\Interfaces\IFLTranslations;
8
use kalanis\kw_files\Interfaces\IProcessFiles;
9
use kalanis\kw_files\Traits\TFile;
10
use kalanis\kw_mime\Interfaces\IMimeChecks;
11
use kalanis\kw_mime\Interfaces\IMiTranslations;
12
use kalanis\kw_mime\MimeException;
13
use kalanis\kw_paths\ArrayPath;
14
use kalanis\kw_paths\PathsException;
15
16
17
/**
18
 * Class DataFiles
19
 * @package kalanis\kw_mime\Check
20
 * System library to detect the mime type by files processor
21
 */
22
class DataFiles implements IMimeChecks
23
{
24
    use Traits\TResult;
25
    use Traits\TToString;
26
    use TFile;
27
28
    /** @var ArrayPath */
29
    protected $pathLib = null;
30
31 6
    public function __construct(?IFLTranslations $flLang = null, ?IMiTranslations $miLang = null)
32
    {
33 6
        $this->setLang($flLang);
34 6
        $this->setMiLang($miLang);
35 6
        $this->pathLib = new ArrayPath();
36 6
    }
37
38 5
    public function canUse($source): bool
39
    {
40 5
        if (!$this->isMimeFunction()) {
41 1
            return false;
42
        }
43 4
        if (is_object($source) && ($source instanceof IProcessFiles)) {
44 3
            $this->setProcessFile($source);
45 3
            return true;
46
        }
47 1
        return false;
48
    }
49
50 3
    public function getMime(array $path): string
51
    {
52
        try {
53 3
            $target = $this->pathLib->setArray($path)->getString();
54 3
            $content = $this->getProcessFile()->readFile($path);
55 2
            $fileData = 'data://,' . urlencode($this->readSourceToString($target, $content));
56 2
            return $this->determineResult(mime_content_type($fileData));
57 1
        } catch (FilesException | PathsException $ex) {
58 1
            throw new MimeException($ex->getMessage(), $ex->getCode(), $ex);
59
        }
60
    }
61
}
62