Completed
Push — master ( f49b06...6d18b2 )
by Iman
01:39
created

WidgetRenderer::generateHtml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 17
rs 9.4285
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils;
4
5
use Illuminate\Contracts\Debug\ExceptionHandler;
6
7
class WidgetRenderer
8
{
9
    public $html;
10
11
    private $_viewData;
12
13
    private $_policies;
14
15
    /**
16
     * BaseWidget constructor.
17
     */
18
    public function __construct()
19
    {
20
        $this->_policies = app(Policies::class);
21
    }
22
23
    /**
24
     * @param $widget object|string
25
     * @param array $args
26
     * @return string
27
     */
28
    public function renderWidget($widget, ...$args)
29
    {
30
        if (is_string($widget)) {
31
            $widget = $this->makeWidgetObj($widget);
32
        }
33
34
        app(Normalizer::class)->normalizeWidgetConfig($widget);
35
36
        try {
37
            $html = $this->generateHtml($widget, ...$args);
38
        } catch (\Exception $e) {
39
            return app()->make(ExceptionHandler::class)->render(app('request'), $e)->send();
40
        }
41
42
        return $html;
43
    }
44
45
    /**
46
     * @param $widget object
47
     * @return \Illuminate\Foundation\Application|mixed
48
     */
49
    private function makeWidgetObj($widget)
50
    {
51
        if (starts_with($widget, ['\\'])) {
52
            return app($widget);
53
        }
54
55
        $widget = app()->getNamespace().'Widgets\\'.$widget;
56
57
        return app($widget);
58
    }
59
60
    /**
61
     * It tries to get the html from cache if possible, otherwise generates it.
62
     *
63
     * @param $widget object
64
     * @param array ...$args
65
     *
66
     * @return string
67
     */
68
    private function generateHtml($widget, ...$args)
69
    {
70
        // Everything inside this function is executed only when the cache is not available.
71
        $expensivePhpCode = function () use ($widget, $args) {
72
            $this->makeDataForView($widget, $args);
73
74
            // render the template with the resulting data.
75
            return $this->renderTemplate($widget);
76
        };
77
78
        // We first try to get the output from the cache before trying to run the expensive $expensivePhpCode...
79
        if ($this->_policies->widgetShouldUseCache($widget->cacheLifeTime)) {
80
            return app(Cache::class)->cacheResult($args, $expensivePhpCode, $widget);
81
        }
82
83
        return $expensivePhpCode();
84
    }
85
86
    /**
87
     * @param $widget object
88
     * @param $args array
89
     *
90
     * @return null
91
     */
92
    private function makeDataForView($widget, array $args)
93
    {
94
        // Here we call the data method on the widget class.
95
        $viewData = \App::call($widget->controller, ...$args);
96
97
        if (($widget->presenter)) {
98
            // We make an object and call the `present` method on it.
99
            // Piping the data through the presenter before sending it to view.
100
            $viewData = \App::call($widget->presenter, [$viewData]);
101
        }
102
103
        $this->_viewData = $viewData;
104
    }
105
106
    /**
107
     * @param $widget object
108
     * @return string HTML output
109
     */
110
    private function renderTemplate($widget)
111
    {
112
        // Here we render the view file to raw html.
113
        $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...
114
115
        // We try to minify the html before storing it in cache to save space.
116
        if ($this->_policies->widgetShouldBeMinified()) {
117
            $this->html = app(HtmlMinifier::class)->minify($this->html);
118
        }
119
120
        // We add some HTML comments before and after the widget output
121
        // So then, we will be able to easily identify the widget in browser's developer tool.
122
        if ($this->_policies->widgetShouldHaveDebugInfo()) {
123
            $this->html = app(DebugInfo::class)->addIdentifierToHtml($widget, $this->html);
124
        }
125
126
        return $this->html;
127
    }
128
}
129