Completed
Pull Request — master (#24)
by Simon
02:59
created

MenuItemController::getUri()   C

Complexity

Conditions 7
Paths 17

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 6.7273
cc 7
eloc 16
nc 17
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
    public function create(Menu $menu)
33
    {
34
        $pages = $this->page->all();
35
36
        return view('menu::admin.menuitems.create', compact('menu', 'pages'));
37
    }
38
39 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...
40
    {
41
        $this->menuItem->create($this->addMenuId($menu, $request));
42
43
        flash(trans('menu::messages.menuitem created'));
44
45
        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...
46
    }
47
48
    public function edit(Menu $menu, Menuitem $menuItem)
49
    {
50
        $pages = $this->page->all();
51
52
        return view('menu::admin.menuitems.edit', compact('menu', 'menuItem', 'pages'));
53
    }
54
55 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...
56
    {
57
        $this->menuItem->update($menuItem, $this->addMenuId($menu, $request));
58
59
        flash(trans('menu::messages.menuitem updated'));
60
61
        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...
62
    }
63
64
    public function destroy(Menu $menu, Menuitem $menuItem)
65
    {
66
        $this->menuItem->destroy($menuItem);
67
68
        flash(trans('menu::messages.menuitem deleted'));
69
70
        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...
71
    }
72
73
    /**
74
     * @param  Menu                                    $menu
75
     * @param  \Illuminate\Foundation\Http\FormRequest $request
76
     * @return array
77
     */
78
    private function addMenuId(Menu $menu, FormRequest $request)
79
    {
80
        $data = $request->all();
81
82
        foreach (\LaravelLocalization::getSupportedLanguagesKeys() as $lang) {
83
            $uri = $data[$lang]['uri'];
84
            $data[$lang]['uri'] = ! empty($uri) ? $uri : $this->getUri($data['page_id'], $lang);
85
        }
86
87
        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...
88
    }
89
90
    /**
91
     * Get uri
92
     *
93
     * @param $item
94
     * @return mixed
95
     */
96
    private function getUri($pageId, $lang)
97
    {
98
        $linkPathArray = array();
99
100
        array_push($linkPathArray, $this->getPageSlug($pageId, $lang));
101
102
        $hasParentItem = true;
103
104
        while ($hasParentItem) {
105
            $pageId = isset($parentItem) ? $parentItem->parent_id : $pageId;
106
107
            $parentItem = Menuitem::where('id', '=', $pageId)->first();
108
109
            if ($parentItem->is_root != true) {
110
                if (!empty($parentItem->page_id)) {
111
                    array_push($linkPathArray, $this->getPageSlug($parentItem->page_id, $lang));
112
                } else {
113
                    $parentUri = ! is_null($parentItem->uri) ? $parentItem->uri . '/' . $linkPathArray : $linkPathArray;
114
                    array_push($linkPathArray, $parentUri);
115
                }
116
            }
117
118
            $hasParentItem = ! is_null($parentItem->parent_id) ? true : false;
119
        }
120
121
        $parentLinkPath = implode('/', array_reverse($linkPathArray));
122
123
        return $parentLinkPath;
124
    }
125
126
    /**
127
     * Get page slug
128
     *
129
     * @params $pageId, $lang
130
     * @return string
131
     */
132
    private function getPageSlug($pageId, $lang)
133
    {
134
        return Page::where('id', '=', $pageId)->first()->translate($lang)->slug;
135
    }
136
}
137