Passed
Push — master ( 1a6375...8f4058 )
by Petr
08:21 queued 05:12
created

TToLocalFile   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 16
c 1
b 0
f 0
dl 0
loc 37
rs 10
ccs 12
cts 12
cp 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A readSourceToLocalFile() 0 27 6
1
<?php
2
3
namespace kalanis\kw_mime\Check\Traits;
4
5
6
use kalanis\kw_mime\MimeException;
7
8
9
/**
10
 * Trait TToLocalFile
11
 * @package kalanis\kw_mime\Check\Traits
12
 */
13
trait TToLocalFile
14
{
15
    use TCheckCalls;
16
17
    /**
18
     * @param string $sourcePath
19
     * @param string|resource|bool|null $sourceData
20
     * @param string $targetLocalFile
21
     * @throws MimeException
22
     */
23 6
    protected function readSourceToLocalFile(string $sourcePath, $sourceData, string $targetLocalFile): void
24
    {
25 6
        if ((false === $sourceData) || (null === $sourceData)) {
26 2
            throw new MimeException($this->getMiLang()->miCannotLoadFile($sourcePath));
27
        }
28
29 4
        $stream = fopen($targetLocalFile, 'rb+');
30 4
        if (false === $stream) {
31
            // @codeCoverageIgnoreStart
32
            throw new MimeException($this->getMiLang()->miCannotLoadTempFile());
33
        }
34
        // @codeCoverageIgnoreEnd
35
36 4
        if (is_resource($sourceData)) {
37 1
            rewind($sourceData);
38 1
            if (false === stream_copy_to_stream($sourceData, $stream, -1, 0)) {
39
                // @codeCoverageIgnoreStart
40
                throw new MimeException($this->getMiLang()->miCannotGetFilePart($sourcePath));
41
            }
42
            // @codeCoverageIgnoreEnd
43
44 1
            rewind($stream);
45 1
            fclose($stream);
46
47
        } else {
48 3
            fwrite($stream, strval($sourceData));
49 3
            fclose($stream);
50
        }
51
    }
52
}
53