Completed
Push — master ( b1984c...7df504 )
by Iman
02:16
created

WidgetRenderer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 94
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A renderWidget() 0 10 2
A generateHtml() 0 16 2
A prepareDataForView() 0 13 2
A renderTemplate() 0 19 3
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils;
4
5
use Illuminate\Contracts\Debug\ExceptionHandler;
6
7
8
class WidgetRenderer
9
{
10
    public $html;
11
    private $viewData;
12
    private $policies;
13
14
    /**
15
     * BaseWidget constructor.
16
     */
17
    public function __construct()
18
    {
19
        $this->policies = app('imanghafoori.widget.policies');
20
    }
21
22
    /**
23
     * @param $widget
24
     * @param array $args
25
     * @return string
26
     */
27
    public function renderWidget($widget, ...$args)
28
    {
29
        app('imanghafoori.widget.normalizer')->normalizeWidgetConfig($widget);
30
        try {
31
            $html = $this->generateHtml($widget, ...$args);
32
        } catch (\Exception $e) {
33
            return app()->make(ExceptionHandler::class)->render(app('request'), $e)->send();
34
        }
35
        return $html;
36
    }
37
38
    /**
39
     * It tries to get the html from cache if possible, otherwise generates it.
40
     * @param $widget
41
     * @param array ...$args
42
     * @return string
43
     */
44
    private function generateHtml($widget, ...$args)
45
    {
46
        // Everything inside this function is executed only when the cache is not available.
47
        $expensivePhpCode = function () use ($widget, $args) {
48
            $this->prepareDataForView($widget, $args);
49
            // render the template with the resulting data.
50
            return $this->renderTemplate($widget);
51
        };
52
53
        // We first try to get the output from the cache before trying to run the expensive $expensivePhpCode...
54
        if ($this->policies->widgetShouldUseCache($widget->cacheLifeTime)) {
55
            return app('imanghafoori.widget.cache')->cacheResult($args, $expensivePhpCode, $widget);
56
        }
57
58
        return $expensivePhpCode();
59
    }
60
61
62
    /**
63
     * @param $widget
64
     * @param $args
65
     * @return null
66
     */
67
    private function prepareDataForView($widget, $args)
68
    {
69
        // Here we call the data method on the widget class.
70
        $viewData = \App::call($widget->controller, $args);
71
72
        if (($widget->presenter)) {
73
            // We make an object and call the `present` method on it.
74
            // Piping the data through the presenter before sending it to view.
75
            $viewData = \App::call($widget->presenter, [$viewData]);
76
        }
77
78
        $this->viewData = $viewData;
79
    }
80
81
    private function renderTemplate($widget)
82
    {
83
        // Here we render the view file to raw html.
84
        $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...
85
86
        // We try to minify the html before storing it in cache to save space.
87
        if ($this->policies->widgetShouldBeMinified()) {
88
            $this->html = app('imanghafoori.widget.minifier')->minify($this->html);
89
        }
90
91
        // We add some HTML comments before and after the widget output
92
        // So then, we will be able to easily identify the widget in browser's developer tool.
93
        if ($this->policies->widgetShouldHaveDebugInfo()) {
94
            $this->html = app('imanghafoori.widget.debugInfo')->addIdentifierToHtml($widget, $this->html);
95
        }
96
97
98
        return $this->html;
99
    }
100
101
}
102