Insertable::insertAfter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php namespace NukaCode\Menu\Traits;
2
3
/**
4
 * Class Insertable
5
 *
6
 * @package NukaCode\Menu\Traits
7
 */
8
trait Insertable {
9
10
    /**
11
     * Let the add method know not to append this item as it was spliced into the array.
12
     *
13
     * @var bool
14
     */
15
    public $insert = false;
16
17
    /**
18
     * Insert this item after another item in the menu
19
     *
20
     * @param $slug
21
     */
22 2
    public function insertAfter($slug)
23
    {
24 2
        $this->insertObject($slug, true);
25 2
    }
26
27
    /**
28
     * Insert this item before another item in the menu
29
     *
30
     * @param $slug
31
     */
32 2
    public function insertBefore($slug)
33
    {
34 2
        $this->insertObject($slug);
35 2
    }
36
37
    /**
38
     * @param $slug
39
     * @param $after
40
     */
41 4
    private function insertObject($slug, $after = false)
42
    {
43 4
        $this->insert = true;
44
45 4
        foreach ($this->menu->links as $linkKey => $link) {
46 4
            if ($link->isDropdown()) {
47 2
                foreach ($link->links as $subLinkKey => $subLink) {
48 2
                    if ($subLink->slug == $slug) {
49 2
                        if ($after) {
50 1
                            $link->links->splice($subLinkKey + 1, 0, [$this]);
51 1
                        } else {
52 1
                            $link->links->splice($subLinkKey, 0, [$this]);
53
                        }
54 2
                    }
55
56 2
                }
57 2
            } else {
58 2
                if ($link->slug == $slug) {
59 2
                    if ($after) {
60 1
                        $this->menu->links->splice($linkKey + 1, 0, [$this]);
61 1
                    } else {
62 1
                        $this->menu->links->splice($linkKey, 0, [$this]);
63
                    }
64 2
                }
65
            }
66 4
        }
67
    }
68
}