EloquentMenuItemRepository::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
1
<?php namespace Modules\Menu\Repositories\Eloquent;
2
3
use Illuminate\Database\Eloquent\Builder;
4
use Illuminate\Support\Facades\App;
5
use Illuminate\Support\Facades\DB;
6
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
7
use Modules\Menu\Events\MenuItemWasCreated;
8
use Modules\Menu\Repositories\MenuItemRepository;
9
10
class EloquentMenuItemRepository extends EloquentBaseRepository implements MenuItemRepository
11
{
12
    public function create($data)
13
    {
14
        $menuItem = $this->model->create($data);
15
16
        event(new MenuItemWasCreated($menuItem));
17
18
        return $menuItem;
19
    }
20
21
    public function update($menuItem, $data)
22
    {
23
        $menuItem->update($data);
24
25
        return $menuItem;
26
    }
27
28
    /**
29
     * Get online root elements
30
     *
31
     * @param  int    $menuId
32
     * @return object
33
     */
34 View Code Duplication
    public function rootsForMenu($menuId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        return $this->model->whereHas('translations', function (Builder $q) {
37
            $q->where('status', 1);
38
            $q->where('locale', App::getLocale());
39
        })->with('translations')->whereMenuId($menuId)->orderBy('position')->get();
40
    }
41
42
    /**
43
     * Get all root elements
44
     *
45
     * @param  int    $menuId
46
     * @return object
47
     */
48
    public function allRootsForMenu($menuId)
49
    {
50
        return $this->model->with('translations')->whereMenuId($menuId)->orderBy('parent_id')->orderBy('position')->get();
51
    }
52
53
    /**
54
     * Get Items to build routes
55
     *
56
     * @return Array
57
     */
58
    public function getForRoutes()
59
    {
60
        $menuitems = DB::table('menu__menus')
61
            ->select(
62
                'primary',
63
                'menu__menuitems.id',
64
                'menu__menuitems.parent_id',
65
                'menu__menuitems.module_name',
66
                'menu__menuitem_translations.uri',
67
                'menu__menuitem_translations.locale'
68
            )
69
            ->join('menu__menuitems', 'menu__menus.id', '=', 'menu__menuitems.menu_id')
70
            ->join('menu__menuitem_translations', 'menu__menuitems.id', '=', 'menu__menuitem_translations.menuitem_id')
71
            ->where('uri', '!=', '')
72
            ->where('module_name', '!=', '')
73
            ->where('status', '=', 1)
74
            ->where('primary', '=', 1)
75
            ->orderBy('module_name')
76
            ->get();
77
78
        $menuitemsArray = [];
79
        foreach ($menuitems as $menuitem) {
80
            $menuitemsArray[$menuitem->module_name][$menuitem->locale] = $menuitem->uri;
81
        }
82
83
        return $menuitemsArray;
84
    }
85
86
    /**
87
     * Get the root menu item for the given menu id
88
     *
89
     * @param  int    $menuId
90
     * @return object
91
     */
92
    public function getRootForMenu($menuId)
93
    {
94
        return $this->model->with('translations')->where(['menu_id' => $menuId, 'is_root' => true])->firstOrFail();
95
    }
96
97
    /**
98
     * Return a complete tree for the given menu id
99
     *
100
     * @param  int    $menuId
101
     * @return object
102
     */
103
    public function getTreeForMenu($menuId)
104
    {
105
        $items = $this->rootsForMenu($menuId);
106
107
        return $items->nest();
108
    }
109
110
    /**
111
     * @param  string $uri
112
     * @param  string $locale
113
     * @return object
114
     */
115
    public function findByUriInLanguage($uri, $locale)
116
    {
117
        return $this->model->whereHas('translations', function (Builder $q) use ($locale, $uri) {
118
            $q->where('status', 1);
119
            $q->where('locale', $locale);
120
            $q->where('uri', $uri);
121
        })->with('translations')->first();
122
    }
123
}
124