DebugInfo::getTplPath()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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

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