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

TempFiles::__construct()   A

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
use kalanis\kw_paths\Stuff;
16
17
18
/**
19
 * Class ResourceFiles
20
 * @package kalanis\kw_mime\Check
21
 * System library to detect the mime type by files processor - pass as file in temp dir
22
 */
23
class TempFiles implements IMimeChecks
24
{
25
    use Traits\TResult;
26
    use Traits\TToLocalFile;
27
    use TFile;
28
29
    /** @var ArrayPath */
30
    protected $pathLib = null;
31
32 5
    public function __construct(?IFLTranslations $flLang = null, ?IMiTranslations $miLang = null)
33
    {
34 5
        $this->setLang($flLang);
35 5
        $this->setMiLang($miLang);
36 5
        $this->pathLib = new ArrayPath();
37 5
    }
38
39 4
    public function canUse($source): bool
40
    {
41 4
        if (!$this->isMimeFunction()) {
42 1
            return false;
43
        }
44 3
        if (is_object($source) && ($source instanceof IProcessFiles)) {
45 2
            $this->setProcessFile($source);
46 2
            return true;
47
        }
48 1
        return false;
49
    }
50
51 3
    public function getMime(array $path): string
52
    {
53 3
        $tempFile = tempnam(sys_get_temp_dir(), 'MimeCheck');
54 3
        if (false === $tempFile) {
55
            // @codeCoverageIgnoreStart
56
            throw new MimeException($this->getMiLang()->miCannotLoadTempFile());
57
        }
58
        // @codeCoverageIgnoreEnd
59
60 3
        $this->checkMimeFunction();
61
        try {
62 3
            $content = $this->getProcessFile()->readFile($path);
63 2
            $this->readSourceToLocalFile(Stuff::arrayToPath($path), $content, $tempFile);
64 2
            $mime = mime_content_type($tempFile);
65 1
        } catch (FilesException | PathsException $ex) {
66 1
            throw new MimeException($ex->getMessage(), $ex->getCode(), $ex);
67 2
        } finally {
68 3
            @unlink($tempFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

68
            /** @scrutinizer ignore-unhandled */ @unlink($tempFile);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
69
        }
70 2
        return $this->determineResult($mime);
71
    }
72
}
73