Completed
Push — master ( 38e886...df2920 )
by Oleg
04:55
created

Basic::make()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2.0185
1
<?php
2
namespace Malezha\Menu\Render;
3
4
use Illuminate\Contracts\Container\Container;
5
use Malezha\Menu\Contracts\MenuRender;
6
7
class Basic implements MenuRender
8
{
9
    /**
10
     * @var Container
11
     */
12
    protected $container;
13
14
    /**
15
     * @var array
16
     */
17
    protected $variables = [];
18
19
    /**
20
     * @var string
21
     */
22
    protected $view;
23
24
    /**
25
     * @inheritDoc
26
     */
27 20
    public function __construct(Container $container)
28
    {
29 20
        $this->container = $container;
30 20
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35 5
    public function make($view)
36
    {
37 5
        $view = $this->replaceNameFromBlade($view);
38
39 5
        if (!$template = $this->findFullPath($view)) {
40
            throw new \Exception('View not found');
41
        }
42
43 5
        $this->view = $template;
44
45 5
        return $this;
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51 5
    public function with($params, $value = null)
52
    {
53 5
        if (is_array($params)) {
54 5
            $this->variables = array_merge($this->variables, $params);
55
            
56 5
            return $this;
57
        }
58
        
59
        $this->variables[$params] = $value;
60
        
61
        return $this;
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67 5
    public function render()
68
    {
69 5
        $s = extract($this->variables);
70
71 5
        ob_start();
72 5
        include($this->view);
73 5
        return ob_get_clean();
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79 20
    public function exists($view)
80
    {
81 20
        $view = $this->replaceNameFromBlade($view);
82 20
        return (bool) $this->findFullPath($view);
83
    }
84
85
    /**
86
     * @param string $view
87
     * @return bool|string
88
     */
89 20
    protected function findFullPath($view)
90
    {
91 20
        $paths = $this->container->make('config')->get('menu.paths');
92
        
93 20
        foreach ($paths as $path) {
94 20
            $template = $path . '/' . $view . '.php';
95 20
            if (file_exists($template)) {
96 5
                return $template;
97
            }
98 20
        }
99
100 20
        return false;
101
    }
102
103
    /**
104
     * @param string $view
105
     * @return string
106
     */
107 20
    protected function replaceNameFromBlade($view)
108
    {
109 20
        return preg_replace("/(.*)(::)(.*)/", "$3", $view);
110
    }
111
}