Passed
Pull Request — 2.x (#597)
by Antonio Carlos
05:31
created

ActiveNavigation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 48
ccs 14
cts 17
cp 0.8235
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A compose() 0 27 6
1
<?php
2
3
namespace A17\Twill\Http\ViewComposers;
4
5
use Illuminate\Contracts\View\View;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Arr;
8
9
class ActiveNavigation
10
{
11
    /**
12
     * @var Request
13
     */
14
    protected $request;
15
16
    /**
17
     * @param Request $request
18
     */
19 1
    public function __construct(Request $request)
20
    {
21 1
        $this->request = $request;
22 1
    }
23
24
    /**
25
     * Binds data to the view.
26
     *
27
     * @param View $view
28
     * @return void
29
     */
30 1
    public function compose(View $view)
31
    {
32 1
        if ($this->request->route()) {
33 1
            $routeName = $this->request->route()->getName();
34
35 1
            $activeMenus = explode('.', $routeName);
36
37
            //starts at 1 because first segment of all back route name is 'admin'
38 1
            $view_active_variables['_global_active_navigation'] = $activeMenus[1];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$view_active_variables was never initialized. Although not strictly required by PHP, it is generally a good practice to add $view_active_variables = array(); before regardless.
Loading history...
39
40 1
            if (count($activeMenus) > 2) {
41
                $view_active_variables['_primary_active_navigation'] = $activeMenus[2];
42 1
            } else if (count($this->request->route()->parameters()) > 0) {
43
                $view_active_variables['_primary_active_navigation'] = Arr::first($this->request->route()->parameters());
44
            }
45
46 1
            if (count($activeMenus) > 3) {
47
                $view_active_variables['_secondary_active_navigation'] = $activeMenus[3] !== 'index' ? $activeMenus[3] : $activeMenus[2];
48
            }
49
50 1
            $with = array_merge($view_active_variables, Arr::only($view->getData(), [
51 1
                '_global_active_navigation',
52
                '_primary_active_navigation',
53
                '_secondary_active_navigation',
54
            ]));
55
56 1
            $view->with($with);
57
        }
58 1
    }
59
}
60