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

ActiveNavigation::compose()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.3541

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 10
nop 1
dl 0
loc 27
ccs 11
cts 14
cp 0.7856
crap 6.3541
rs 9.2222
c 0
b 0
f 0
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