Passed
Push — master ( fba3db...2b14a7 )
by Petr
03:12
created

TToStream::toStream()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.0957

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
c 2
b 0
f 0
nc 7
nop 2
dl 0
loc 26
ccs 14
cts 16
cp 0.875
crap 7.0957
rs 8.8333
1
<?php
2
3
namespace kalanis\kw_files\Traits;
4
5
6
use kalanis\kw_files\FilesException;
7
8
9
/**
10
 * Trait TToStream
11
 * @package kalanis\kw_menu\Traits
12
 * Transform resource to stream with handler
13
 */
14
trait TToStream
15
{
16
    use TLang;
17
18
    /**
19
     * @param string $target
20
     * @param mixed $content
21
     * @throws FilesException
22
     * @return resource
23
     */
24 29
    protected function toStream(string $target, $content)
25
    {
26 29
        if (is_null($content)) {
27 1
            throw new FilesException($this->getLang()->flCannotLoadFile($target));
28 28
        } elseif (is_bool($content)) {
29 2
            throw new FilesException($this->getLang()->flCannotLoadFile($target));
30 26
        } elseif (is_object($content)) {
31 2
            throw new FilesException($this->getLang()->flCannotLoadFile($target));
32 24
        } elseif (is_resource($content)) {
33 1
            rewind($content);
34 1
            return $content;
35
        } else {
36 23
            $handle = fopen('php://temp', 'rb+');
37 23
            if (false === $handle) {
38
                // @codeCoverageIgnoreStart
39
                // must die something with stream reading
40
                throw new FilesException($this->getLang()->flCannotLoadFile($target));
41
            }
42
            // @codeCoverageIgnoreEnd
43 23
            if (false === fwrite($handle, $content)) {
44
                // @codeCoverageIgnoreStart
45
                // must die something with stream reading
46
                throw new FilesException($this->getLang()->flCannotLoadFile($target));
47
            }
48
            // @codeCoverageIgnoreEnd
49 23
            return $handle;
50
        }
51
    }
52
}
53