Manager::createDriver()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1
Metric Value
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Larabros\Laranav;
4
5
use Larabros\Laranav\Menus\Menu;
6
use Illuminate\Support\Manager as AbstractManager;
7
use Illuminate\Contracts\Foundation\Application;
8
9
class Manager extends AbstractManager
10
{
11
    /**
12
     * The array of menus as defined in the config files.
13
     *
14
     * @var array
15
     */
16
    protected $menus;
17
18
    /**
19
     * Get a `Menu` instance.
20
     *
21
     * @param  string $name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $name not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
22
     *
23
     * @return Menu
24
     */
25 4
    public function menu($name = null)
26
    {
27 4
        $name = $name ?: $this->getDefaultDriver();
28 4
        return $this->menus[$name] = $this->driver($name);
29
    }
30
31
    /**
32
     * @inheritDoc
33
     */
34 4
    public function getDefaultDriver()
35
    {
36 4
        return 'default';
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42 4
    public function driver($name = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
43
    {
44 4
        return isset($this->menus[$name]) ? $this->menus[$name] : $this->createDriver($name);
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50 4
    protected function createDriver($name)
51
    {
52 4
        $config = array_merge($this->getConfig($this->getDefaultDriver()), $this->getConfig($name));
53 4
        $items  = $this->getMenuItems($name);
54
55 4
        return new Menu(
56 4
            $name,
57 4
            $items,
58 4
            $config,
59 4
            $this->app->make('url'),
60 4
            $this->app->make('view')
61 4
        );
62
    }
63
64
    /**
65
     * Get a menu's configuration.
66
     *
67
     * @param  string  $name
68
     *
69
     * @return array
70
     */
71 4
    protected function getConfig($name)
72
    {
73 4
        return $this->app->make('config')->get("laranav.config.{$name}");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
74
    }
75
76
    /**
77
     * Get a menu's items.
78
     *
79
     * @param  string  $name
80
     *
81
     * @return array
82
     */
83 4
    protected function getMenuItems($name)
84
    {
85 4
        return $this->app->make('config')->get("laranav.menus.{$name}");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
86
    }
87
}
88