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

DebugInfo   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addIdentifierToHtml() 0 8 1
A addDebugInfo() 0 8 2
A cacheState() 0 7 2
A addHtmlComments() 0 4 1
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