Completed
Pull Request — master (#24)
by Simon
09:30
created

MenuItemController::getMenuSelect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 2
eloc 6
nc 2
nop 2
1
<?php namespace Modules\Menu\Http\Controllers\Admin;
2
3
use Illuminate\Foundation\Http\FormRequest;
4
use Laracasts\Flash\Flash;
5
use Modules\Core\Http\Controllers\Admin\AdminBaseController;
6
use Modules\Menu\Entities\Menu;
7
use Modules\Menu\Entities\Menuitem;
8
use Modules\Menu\Http\Requests\CreateMenuItemRequest;
9
use Modules\Menu\Http\Requests\UpdateMenuItemRequest;
10
use Modules\Menu\Repositories\MenuItemRepository;
11
use Modules\Page\Entities\Page;
12
use Modules\Page\Repositories\PageRepository;
13
14
class MenuItemController extends AdminBaseController
15
{
16
    /**
17
     * @var MenuItemRepository
18
     */
19
    private $menuItem;
20
    /**
21
     * @var PageRepository
22
     */
23
    private $page;
24
25
    public function __construct(MenuItemRepository $menuItem, PageRepository $page)
26
    {
27
        parent::__construct();
28
        $this->menuItem = $menuItem;
29
        $this->page = $page;
30
    }
31
32 View Code Duplication
    public function create(Menu $menu)
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...
33
    {
34
        $pages = $this->page->all();
35
36
        $menuSelect = $this->getMenuSelect($menu->all());
37
38
        return view('menu::admin.menuitems.create', compact('menu', 'pages', 'menuSelect'));
39
    }
40
41 View Code Duplication
    public function store(Menu $menu, CreateMenuItemRequest $request)
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...
42
    {
43
        $this->menuItem->create($this->addMenuId($menu, $request));
44
45
        flash(trans('menu::messages.menuitem created'));
46
47
        return redirect()->route('admin.menu.menu.edit', [$menu->id]);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Menu\Entities\Menu>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
48
    }
49
50 View Code Duplication
    public function edit(Menu $menu, Menuitem $menuItem)
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...
51
    {
52
        $pages = $this->page->all();
53
54
        $menuSelect = $this->getMenuSelect($menu, $menuItem->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Menu\Entities\Menuitem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
56
        return view('menu::admin.menuitems.edit', compact('menu', 'menuItem', 'pages', 'menuSelect'));
57
    }
58
59 View Code Duplication
    public function update(Menu $menu, Menuitem $menuItem, UpdateMenuItemRequest $request)
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...
60
    {
61
        $this->menuItem->update($menuItem, $this->addMenuId($menu, $request));
62
63
        flash(trans('menu::messages.menuitem updated'));
64
65
        return redirect()->route('admin.menu.menu.edit', [$menu->id]);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Menu\Entities\Menu>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
66
    }
67
68
    public function destroy(Menu $menu, Menuitem $menuItem)
69
    {
70
        $this->menuItem->destroy($menuItem);
71
72
        flash(trans('menu::messages.menuitem deleted'));
73
74
        return redirect()->route('admin.menu.menu.edit', [$menu->id]);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Menu\Entities\Menu>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
75
    }
76
77
    /**
78
     * @param Menu, $menuItemId
0 ignored issues
show
Documentation introduced by
The doc-type Menu, could not be parsed: Expected "|" or "end of type", but got "," at position 4. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
79
     * @return array
80
     */
81
    private function getMenuSelect($menu, $menuItemId = null)
0 ignored issues
show
Unused Code introduced by
The parameter $menuItemId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        $menus = $menu->all();
84
85
        $menuSelect = array();
86
87
        foreach ($menus as $menuEntity) {
88
            $menuSelect[$menuEntity['name']] = $menuEntity->menuitems()->get();
89
        }
90
91
        return $menuSelect;
92
    }
93
94
    /**
95
     * @param  Menu                                    $menu
96
     * @param  \Illuminate\Foundation\Http\FormRequest $request
97
     * @return array
98
     */
99
    private function addMenuId(Menu $menu, FormRequest $request)
100
    {
101
        $data = $request->all();
102
103
        foreach (\LaravelLocalization::getSupportedLanguagesKeys() as $lang) {
104
            $uri = $data[$lang]['uri'];
105
            $data[$lang]['uri'] = ! empty($uri) ? $uri : $this->getUri($data['page_id'], $lang);
106
        }
107
108
        return array_merge($data, ['menu_id' => $menu->id]);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Menu\Entities\Menu>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
109
    }
110
111
    /**
112
     * Get uri
113
     *
114
     * @param $item
115
     * @return string
116
     */
117
    private function getUri($pageId, $lang)
118
    {
119
        $linkPathArray = array();
120
121
        array_push($linkPathArray, $this->getPageSlug($pageId, $lang));
122
123
        $hasParentItem = true;
124
125
        while ($hasParentItem) {
126
            $pageId = isset($parentItem) ? $parentItem->parent_id : $pageId;
127
128
            $parentItem = Menuitem::where('id', '=', $pageId)->first();
129
130
            if ($parentItem->is_root != true) {
131
                if (!empty($parentItem->page_id)) {
132
                    array_push($linkPathArray, $this->getPageSlug($parentItem->page_id, $lang));
133
                } else {
134
                    $parentUri = ! is_null($parentItem->uri) ? $parentItem->uri . '/' . $linkPathArray : $linkPathArray;
135
                    array_push($linkPathArray, $parentUri);
136
                }
137
            }
138
139
            $hasParentItem = ! is_null($parentItem->parent_id) ? true : false;
140
        }
141
142
        $parentLinkPath = implode('/', array_reverse($linkPathArray));
143
144
        return $parentLinkPath;
145
    }
146
147
    /**
148
     * Get page slug
149
     *
150
     * @params $pageId, $lang
151
     * @return string
152
     */
153
    private function getPageSlug($pageId, $lang)
154
    {
155
        return Page::where('id', '=', $pageId)->first()->translate($lang)->slug;
156
    }
157
}
158