DataFiles::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
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
    protected ArrayPath $pathLib;
29
30 6
    public function __construct(?IFLTranslations $flLang = null, ?IMiTranslations $miLang = null)
31
    {
32 6
        $this->setFlLang($flLang);
33 6
        $this->setMiLang($miLang);
34 6
        $this->pathLib = new ArrayPath();
35
    }
36
37 5
    public function canUse($source): bool
38
    {
39 5
        if (!$this->isMimeFunction()) {
40 1
            return false;
41
        }
42 4
        if (is_object($source) && ($source instanceof IProcessFiles)) {
43 3
            $this->setProcessFile($source);
44 3
            return true;
45
        }
46 1
        return false;
47
    }
48
49 3
    public function getMime(array $path): string
50
    {
51
        try {
52 3
            $target = $this->pathLib->setArray($path)->getString();
53 3
            $content = $this->getProcessFile()->readFile($path);
54 2
            $fileData = 'data://,' . urlencode($this->readSourceToString($target, $content));
55 2
            return $this->determineResult(mime_content_type($fileData));
56 1
        } catch (FilesException | PathsException $ex) {
57 1
            throw new MimeException($ex->getMessage(), $ex->getCode(), $ex);
58
        }
59
    }
60
}
61