Passed
Push — master ( 5ebdc1...038fc1 )
by Gabriel
15:21
created

CanRenderTrait::loadWithFallback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
c 1
b 0
f 1
nc 2
nop 4
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Nip\View\Traits;
4
5
/**
6
 * Class CanRenderTrait
7
 * @package Nip\View\Traits
8
 */
9
trait CanRenderTrait
10
{
11
    /**
12
     * @param bool|Callable $condition
13
     * @param $view
14
     * @param array $variables
15
     * @param bool $return
16
     * @return bool|string|void|null
17
     */
18 2
    public function loadIf($condition, $view, $variables = [], $return = false)
19
    {
20 2
        $condition = is_callable($condition) ? $condition() : $condition;
21 2
        if ($condition == false) {
22 2
            return;
23
        }
24 2
        return $this->load($view, $variables, $return);
25
    }
26
27
    /**
28
     * @param $view
29
     * @param array $variables
30
     * @param bool $return
31
     * @return bool|string|void|null
32
     */
33 1
    public function loadIfExists($view, $variables = [], $return = false)
34
    {
35 1
        return $this->loadIf($this->existPath($view), $view, $variables, $return);
0 ignored issues
show
Bug introduced by
It seems like existPath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

35
        return $this->loadIf($this->/** @scrutinizer ignore-call */ existPath($view), $view, $variables, $return);
Loading history...
36
    }
37
38
    /**
39
     * @param $view
40
     * @param array $variables
41
     * @param bool $return
42
     * @return bool|string|void|null
43
     */
44
    public function loadWithFallback($view, $fallback, $variables = [], $return = false)
45 4
    {
46
        $view = $this->existPath($view) ? $view : $fallback;
47 4
        return $this->load( $view, $variables, $return);
48
    }
49 4
50
    /** @noinspection PhpInconsistentReturnPointsInspection
51
     *
52
     * @param $view
53 4
     * @param array $variables
54
     * @param bool $return
55 4
     * @return string|boolean
56
     */
57
    public function load($view, $variables = [], $return = false)
58
    {
59
        $html = $this->getContents($view, $variables);
60
61
        if ($return === true) {
62
            return $html;
63 4
        }
64
65 4
        echo $html;
66
67 4
        return null;
68
    }
69 4
70 4
    /**
71
     * @param $view
72 4
     * @param array $variables
73 4
     * @return string
74 4
     */
75
    public function getContents($view, $variables = [])
76 4
    {
77
        extract($variables);
78
79
        $path = $this->buildPath($view);
0 ignored issues
show
Bug introduced by
It seems like buildPath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

79
        /** @scrutinizer ignore-call */ 
80
        $path = $this->buildPath($view);
Loading history...
80
81
        unset($view, $variables);
82
        ob_start();
83
        /** @noinspection PhpIncludeInspection */
84
        include($path);
85
        $html = ob_get_contents();
86
        ob_end_clean();
87
88
        return $html;
89
    }
90
}
91