Completed
Pull Request — 2.0 (#31)
by Nicolas
02:57
created

MenuItemController::getUri()   C

Complexity

Conditions 8
Paths 3

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 5.3846
cc 8
eloc 17
nc 3
nop 2
1
<?php
2
3
namespace Modules\Menu\Http\Controllers\Admin;
4
5
use Illuminate\Foundation\Http\FormRequest;
6
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
7
use Modules\Core\Http\Controllers\Admin\AdminBaseController;
8
use Modules\Menu\Entities\Menu;
9
use Modules\Menu\Entities\Menuitem;
10
use Modules\Menu\Http\Requests\CreateMenuItemRequest;
11
use Modules\Menu\Http\Requests\UpdateMenuItemRequest;
12
use Modules\Menu\Repositories\MenuItemRepository;
13
use Modules\Page\Repositories\PageRepository;
14
15
class MenuItemController extends AdminBaseController
16
{
17
    /**
18
     * @var MenuItemRepository
19
     */
20
    private $menuItem;
21
    /**
22
     * @var PageRepository
23
     */
24
    private $page;
25
26
    public function __construct(MenuItemRepository $menuItem, PageRepository $page)
27
    {
28
        parent::__construct();
29
        $this->menuItem = $menuItem;
30
        $this->page = $page;
31
    }
32
33
    public function create(Menu $menu)
34
    {
35
        $pages = $this->page->all();
36
37
        $menuSelect = $this->getMenuSelect($menu);
38
39
        return view('menu::admin.menuitems.create', compact('menu', 'pages', 'menuSelect'));
40
    }
41
42 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...
43
    {
44
        $this->menuItem->create($this->addMenuId($menu, $request));
45
46
        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...
47
            ->withSuccess(trans('menu::messages.menuitem created'));
48
    }
49
50
    public function edit(Menu $menu, Menuitem $menuItem)
51
    {
52
        $pages = $this->page->all();
53
54
        $menuSelect = $this->getMenuSelect($menu);
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
        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...
64
            ->withSuccess(trans('menu::messages.menuitem updated'));
65
    }
66
67
    public function destroy(Menu $menu, Menuitem $menuItem)
68
    {
69
        $this->menuItem->destroy($menuItem);
70
71
        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...
72
            ->withSuccess(trans('menu::messages.menuitem deleted'));
73
    }
74
75
    /**
76
     * @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...
Bug introduced by
There is no parameter named $menuItemId. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
77
     * @return array
78
     */
79
    private function getMenuSelect($menu)
80
    {
81
        return $menu->menuitems()->where('is_root', '!=', true)->get()->nest()->listsFlattened('title');
82
    }
83
84
    /**
85
     * @param  Menu $menu
86
     * @param  \Illuminate\Foundation\Http\FormRequest $request
87
     * @return array
88
     */
89
    private function addMenuId(Menu $menu, FormRequest $request)
90
    {
91
        $data = $request->all();
92
93
        foreach (LaravelLocalization::getSupportedLanguagesKeys() as $lang) {
94
            if ($data['link_type'] === 'page' && ! empty($data['page_id'])) {
95
                $data[$lang]['uri'] = $this->getUri($data['page_id'], $lang);
96
            }
97
        }
98
99
        if (empty($data['parent_id'])) {
100
            $data['parent_id'] = $this->menuItem->getRootForMenu($menu->id)->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...
101
        }
102
103
        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...
104
    }
105
106
    /**
107
     * Get uri
108
     * @param $pageId
109
     * @param $lang
110
     * @return string
111
     */
112
    private function getUri($pageId, $lang)
113
    {
114
        $linkPathArray = array();
115
116
        array_push($linkPathArray, $this->getPageSlug($pageId, $lang));
117
118
        $currentItem = $this->menuItem->getByAttributes(['page_id' => $pageId])->first();
119
120
        if ($currentItem !== null) {
121
            $hasParentItem = !(is_null($currentItem->parent_id)) ? true : false;
122
123
            while ($hasParentItem) {
124
                $parentItemId = isset($parentItem) ? $parentItem->parent_id : $currentItem->parent_id;
125
126
                $parentItem = $this->menuItem->find($parentItemId);
127
128
                if ($parentItem->is_root != true) {
129
                    if (!empty($parentItem->page_id)) {
130
                        array_push($linkPathArray, $this->getPageSlug($parentItem->page_id, $lang));
131
                    } else {
132
                        array_push($linkPathArray, $this->getParentUri($parentItem, $linkPathArray));
133
                    }
134
                }
135
136
                $hasParentItem = !is_null($parentItem->parent_id) ? true : false;
137
            }
138
        }
139
140
        $parentLinkPath = implode('/', array_reverse($linkPathArray));
141
142
        return $parentLinkPath;
143
    }
144
145
    /**
146
     * Get page slug
147
     *
148
     * @params $pageId, $lang
149
     * @return string
150
     */
151
    private function getPageSlug($id, $lang)
152
    {
153
        $page = $this->page->find($id);
154
        $translation = $page->translate($lang);
155
156
        if ($translation === null) {
157
            return $page->translate(config('app.fallback_locale'))->slug;
158
        }
159
160
        return $translation->slug;
161
    }
162
163
    /**
164
     * Get parent uri
165
     *
166
     * @params $pageId, $lang
167
     * @return string
168
     */
169
    private function getParentUri($item, $linkPathArray)
170
    {
171
        return ! is_null($item->uri) ? $item->uri . '/' . implode('/', $linkPathArray) : implode('/', $linkPathArray);
172
    }
173
}
174