Passed
Push — master ( 51c2a5...5c54bc )
by Petr
08:51
created

TToStream   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 36
ccs 14
cts 14
cp 1
rs 10
c 2
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B toStream() 0 26 7
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 13
    protected function toStream(string $target, $content)
25
    {
26 13
        if (is_null($content)) {
27 1
            throw new FilesException($this->getFlLang()->flCannotLoadFile($target));
28 12
        } elseif (is_bool($content)) {
29 2
            throw new FilesException($this->getFlLang()->flCannotLoadFile($target));
30 10
        } elseif (is_object($content)) {
31 2
            throw new FilesException($this->getFlLang()->flCannotLoadFile($target));
32 8
        } elseif (is_resource($content)) {
33 1
            rewind($content);
34 1
            return $content;
35
        } else {
36 7
            $handle = fopen('php://temp', 'rb+');
37 7
            if (false === $handle) {
38
                // @codeCoverageIgnoreStart
39
                // must die something with stream reading
40
                throw new FilesException($this->getFlLang()->flCannotLoadFile($target));
41
            }
42
            // @codeCoverageIgnoreEnd
43 7
            if (false === fwrite($handle, strval($content))) {
44
                // @codeCoverageIgnoreStart
45
                // must die something with stream reading
46
                throw new FilesException($this->getFlLang()->flCannotLoadFile($target));
47
            }
48
            // @codeCoverageIgnoreEnd
49 7
            return $handle;
50
        }
51
    }
52
}
53