Passed
Push — master ( 728467...6349cc )
by Petr
07:23
created

TToString::toString()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

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