Completed
Push — master ( 0b019b...a04461 )
by Iman
02:16
created

WidgetRenderer::makeDataForView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils;
4
5
use Illuminate\Contracts\Debug\ExceptionHandler;
6
7
class WidgetRenderer
8
{
9
    public $html;
10
    private $_viewData;
11
    private $_policies;
12
13
    /**
14
     * BaseWidget constructor.
15
     */
16
    public function __construct()
17
    {
18
        $this->_policies = app(Policies::class);
19
    }
20
21
    /**
22
     * @param $widget object|string
23
     * @param array $args
24
     * @return string
25
     */
26
    public function renderWidget($widget, ...$args)
27
    {
28
        if (is_string($widget)) {
29
            $widget = $this->_makeWidgetObj($widget);
30
        }
31
32
        app(Normalizer::class)->normalizeWidgetConfig($widget);
33
34
        try {
35
            $html = $this->_generateHtml($widget, ...$args);
36
        } catch (\Exception $e) {
37
            return app()->make(ExceptionHandler::class)->render(app('request'), $e)->send();
38
        }
39
40
        return $html;
41
    }
42
43
    /**
44
     * It tries to get the html from cache if possible, otherwise generates it.
45
     *
46
     * @param $widget object
47
     * @param array ...$args
48
     *
49
     * @return string
50
     */
51
    private function _generateHtml($widget, ...$args)
52
    {
53
        // Everything inside this function is executed only when the cache is not available.
54
        $expensivePhpCode = function () use ($widget, $args) {
55
            $this->makeDataForView($widget, $args);
56
            // render the template with the resulting data.
57
            return $this->renderTemplate($widget);
58
        };
59
60
        // We first try to get the output from the cache before trying to run the expensive $expensivePhpCode...
61
        if ($this->_policies->widgetShouldUseCache($widget->cacheLifeTime)) {
62
            return app(Cache::class)->cacheResult($args, $expensivePhpCode, $widget);
63
        }
64
65
        return $expensivePhpCode();
66
    }
67
68
    /**
69
     * @param $widget object
70
     * @param $args array
71
     *
72
     * @return null
73
     */
74
    private function makeDataForView($widget, array $args)
75
    {
76
        // Here we call the data method on the widget class.
77
        $viewData = \App::call($widget->controller, ...$args);
78
79
        if (($widget->presenter)) {
80
            // We make an object and call the `present` method on it.
81
            // Piping the data through the presenter before sending it to view.
82
            $viewData = \App::call($widget->presenter, [$viewData]);
83
        }
84
85
        $this->_viewData = $viewData;
86
    }
87
88
    /**
89
     * @param $widget object
90
     * @return string HTML output
91
     */
92
    private function renderTemplate($widget)
93
    {
94
        // Here we render the view file to raw html.
95
        $this->html = view($widget->template, [$widget->contextAs => $this->_viewData])->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
96
97
        // We try to minify the html before storing it in cache to save space.
98
        if ($this->_policies->widgetShouldBeMinified()) {
99
            $this->html = app(HtmlMinifier::class)->minify($this->html);
100
        }
101
102
        // We add some HTML comments before and after the widget output
103
        // So then, we will be able to easily identify the widget in browser's developer tool.
104
        if ($this->_policies->widgetShouldHaveDebugInfo()) {
105
            $this->html = app(DebugInfo::class)->addIdentifierToHtml($widget, $this->html);
106
        }
107
108
        return $this->html;
109
    }
110
111
    /**
112
     * @param $widget object
113
     * @return \Illuminate\Foundation\Application|mixed
114
     */
115
    private function _makeWidgetObj($widget)
116
    {
117
        $widget = app()->getNamespace().'Widgets\\'.$widget;
118
        return app($widget);
119
    }
120
}
121