Passed
Push — master ( a23672...3dee81 )
by Iman
08:02
created

DebugInfo::cacheState()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 3
cts 5
cp 0.6
crap 2.2559
rs 10
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 6
    public function __construct()
17
    {
18 6
        $this->policies = app(Policies::class);
19 6
    }
20
21
    /**
22
     * @param object $widget
23
     * @param string $html
24
     * @return string
25
     */
26 6
    public function addIdentifierToHtml($widget, string $html): string
27
    {
28 6
        $this->widget = $widget;
29 6
        $this->html = $html;
30 6
        $this->addDebugInfo();
31 6
        $this->addHtmlComments();
32
33 6
        return $this->html;
34
    }
35
36
    /**
37
     * Adds debug info to html as HTML title tags.
38
     */
39 6
    private function addDebugInfo(): void
40
    {
41 6
        $tpl = $this->getTplPath($this->widget->template);
42
43 6
        $this->html = "<span title='WidgetObj : ".get_class($this->widget).".php&#013;Template : {$tpl}{$this->cacheState()}'>{$this->html}</span>";
44 6
    }
45
46
    /**
47
     * Generates a string of current cache configurations.
48
     *
49
     * @return string
50
     */
51 6
    private function cacheState(): string
52
    {
53 6
        if (! $this->policies->widgetShouldUseCache()) {
54 6
            return ' &#013; Cache: is globally turned off (You should put "enable_cache" => true in config\widgetize.php) ';
55
        }
56
        $l = $this->widget->cacheLifeTime->i ?? 0;
57
58
        return " &#013;Cache : {$l} (min) ";
59
    }
60
61 6
    private function addHtmlComments(): void
62
    {
63 6
        $this->html = "<!-- '{".get_class($this->widget)."' Widget Start -->".$this->html."<!-- '".get_class($this->widget)."' Widget End -->";
64 6
    }
65
66
    /**
67
     * @param string $tpl
68
     * @return string
69
     */
70 6
    private function getTplPath(string $tpl): string
71
    {
72 6
        if (str_contains($tpl, 'Widgets::')) {
73 1
            $tpl = str_replace('Widgets::', app()->getNamespace().'Widgets\\', $tpl);
0 ignored issues
show
introduced by
The method getNamespace() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            $tpl = str_replace('Widgets::', app()->/** @scrutinizer ignore-call */ getNamespace().'Widgets\\', $tpl);
Loading history...
74
        }
75
76 6
        return str_replace('.', '\\', $tpl).'.blade.php';
77
    }
78
}
79