Passed
Push — master ( 2f2795...71165f )
by Iman
06:01
created

TemplateNormalizer::normalizeTemplateName()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 1
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils\Normalizers;
4
5
class TemplateNormalizer
6
{
7
    /**
8
     * Figures out which template to render.
9
     * @param object $widget
10
     * @return null
11
     */
12 14
    public function normalizeTemplateName($widget)
13
    {
14
        // class name without namespace.
15 14
        $className = str_replace('App\\Widgets\\', '', get_class($widget));
16
17
        // replace slashes with dots
18 14
        $className = str_replace(['\\', '/'], '.', $className);
19
20 14
        if (! property_exists($widget, 'template')) {
21 1
            $widget->template = 'Widgets::'.$className.'View';
22
        }
23
24 14
        if (! view()->exists($widget->template)) {
25
            throw new \InvalidArgumentException("View file \"{$widget->template}\" not found by: '".get_class($widget)." '");
26
        }
27 14
    }
28
}
29