Completed
Push — master ( 909c0d...87861c )
by Iman
01:40
created

DebugInfo::addIdentifierToHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils;
4
5
/**
6
 * Class DebugInfo.
7
 */
8
class DebugInfo
9
{
10
    private $widget;
11
12
    private $html;
13
14
    private $policies;
15
16
    public function __construct()
17
    {
18
        $this->policies = app(Policies::class);
19
    }
20
21
    /**
22
     * @param object $widget
23
     * @param string $html
24
     * @return string
25
     */
26
    public function addIdentifierToHtml($widget, $html)
27
    {
28
        $this->widget = $widget;
29
        $this->html = $html;
30
        $this->addDebugInfo();
31
        $this->addHtmlComments();
32
33
        return $this->html;
34
    }
35
36
    /**
37
     * Adds debug info to html as HTML title tags.
38
     */
39
    private function addDebugInfo()
40
    {
41
        $tpl = $this->getTplPath($this->widget->template);
42
43
        $this->html = "<span title='WidgetObj : ".get_class($this->widget).".php&#013;Template : {$tpl}{$this->cacheState()}'>{$this->html}</span>";
44
    }
45
46
    /**
47
     * Generates a string of current cache configurations.
48
     *
49
     * @return string
50
     */
51
    private function cacheState()
52
    {
53
        if (! $this->policies->widgetShouldUseCache()) {
54
            return ' &#013;Cache: is globally turned off (You should put "enable_cache" => true in config\widgetize.php) ';
55
        }
56
57
        return " &#013;Cache : {$this->widget->cacheLifeTime} (min)' ";
58
    }
59
60
    private function addHtmlComments()
61
    {
62
        $this->html = "<!-- '{".get_class($this->widget)."' Widget Start -->".$this->html."<!-- '".get_class($this->widget)."' Widget End -->";
63
    }
64
65
    /**
66
     * @param $tpl
67
     * @return string
68
     */
69
    private function getTplPath($tpl)
70
    {
71
        if (str_contains($tpl, 'Widgets::')) {
72
            $tpl = str_replace('Widgets::', app()->getNamespace().'Widgets\\', $tpl);
73
        }
74
75
        return str_replace('.', '\\', $tpl).'.blade.php';
76
    }
77
}
78