Completed
Push — master ( a0253d...70910a )
by Ryan
12:32 queued 06:51
created

NavigationSorter::sort()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 40
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 4
nop 1
dl 0
loc 40
rs 8.5806
c 0
b 0
f 0
1
<?php namespace Anomaly\Streams\Platform\Ui\ControlPanel\Component\Navigation;
2
3
use Anomaly\Streams\Platform\Ui\ControlPanel\Component\Navigation\Event\SortNavigation;
4
use Anomaly\Streams\Platform\Ui\ControlPanel\ControlPanelBuilder;
5
use Illuminate\Contracts\Events\Dispatcher;
6
7
/**
8
 * Class NavigationSorter
9
 *
10
 * @link   http://pyrocms.com/
11
 * @author PyroCMS, Inc. <[email protected]>
12
 * @author Ryan Thompson <[email protected]>
13
 */
14
class NavigationSorter
15
{
16
17
    /**
18
     * The event dispatcher.
19
     *
20
     * @var Dispatcher
21
     */
22
    protected $events;
23
24
    /**
25
     * Create a new NavigationSorter instance.
26
     *
27
     * @param Dispatcher $events
28
     */
29
    public function __construct(Dispatcher $events)
30
    {
31
        $this->events = $events;
32
    }
33
34
    /**
35
     * Create a new NavigationSorter instance.
36
     *
37
     * @param ControlPanelBuilder $builder
38
     */
39
    public function sort(ControlPanelBuilder $builder)
40
    {
41
        if (!$navigation = $builder->getNavigation()) {
42
            return;
43
        }
44
45
        ksort($navigation);
46
47
        /**
48
         * Make the namespaces the key now
49
         * that we've applied default sorting.
50
         */
51
        $navigation = array_combine(
52
            array_map(
53
                function ($item) {
54
                    return $item['slug'];
55
                },
56
                $navigation
57
            ),
58
            array_values($navigation)
59
        );
60
61
        /**
62
         * Again by default push the dashboard
63
         * module to the top of the navigation.
64
         */
65
        foreach ($navigation as $key => $module) {
66
67
            if ($key == 'anomaly.module.dashboard') {
68
69
                $navigation = [$key => $module] + $navigation;
70
71
                break;
72
            }
73
        }
74
75
        $builder->setNavigation($navigation);
76
77
        $this->events->fire(new SortNavigation($builder));
78
    }
79
}
80