Completed
Push — master ( eb1ea8...814c2d )
by Iman
03:08
created

DebugInfo::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils;
4
5
/**
6
 * Class DebugInfo
7
 * @package Imanghafoori\Widgets\Utils
8
 */
9
class DebugInfo
10
{
11
    private $widget;
12
    private $html;
13
    private $policies;
14
15
    public function __construct()
16
    {
17
        $this->policies = app('imanghafoori.widget.policies');
18
    }
19
20
    public function addIdentifierToHtml($widget, $html)
21
    {
22
        $this->widget = $widget;
23
        $this->html = $html;
24
        $this->addDebugInfo();
25
        $this->addHtmlComments();
26
        return $this->html;
27
    }
28
29
    private function addDebugInfo()
30
    {
31
        $tpl = $this->widget->template;
32
        if (str_contains($this->widget->template, 'Widgets::')) {
33
            $tpl = str_replace('Widgets::', 'app\Widgets\\', $this->widget->template);
34
        }
35
        $this->html = "<div title='" . get_class($this->widget) . "::class || template : {$tpl}" . $this->cacheState() . "' style='box-shadow: 0px 0px 15px 5px #00c62b inset'>" . $this->html . "</div>";
36
    }
37
38
    /**
39
     * Generates a string of current cache configurations.
40
     * @return string
41
     */
42
    private function cacheState()
43
    {
44
        if (!$this->policies->widgetShouldUseCache()) {
45
            return " || cache: is globally turned off (in .env set WIDGET_CACHE=true) ";
46
        }
47
        return " || cache: {$this->widget->cacheLifeTime}(min)' ";
48
    }
49
50
    private function addHtmlComments()
51
    {
52
        $this->html = "<!-- '{$this->widget->friendlyName}' Widget Start -->" . $this->html . "<!-- '{$this->widget->friendlyName}' Widget End -->";
53
    }
54
}
55