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

MenuServiceProvider::getTarget()   D

Complexity

Conditions 9
Paths 5

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 38
rs 4.9091
cc 9
eloc 25
nc 5
nop 1
1
<?php namespace Modules\Menu\Providers;
2
3
use Illuminate\Support\Facades\Config;
4
use Illuminate\Support\ServiceProvider;
5
use Modules\Menu\Entities\Menu;
6
use Modules\Menu\Entities\Menuitem;
7
use Modules\Menu\Repositories\Cache\CacheMenuDecorator;
8
use Modules\Menu\Repositories\Cache\CacheMenuItemDecorator;
9
use Modules\Menu\Repositories\Eloquent\EloquentMenuItemRepository;
10
use Modules\Menu\Repositories\Eloquent\EloquentMenuRepository;
11
use Modules\Page\Entities\Page;
12
use Pingpong\Menus\MenuBuilder as Builder;
13
use Pingpong\Menus\MenuFacade;
14
use Pingpong\Menus\MenuItem as PingpongMenuItem;
15
16
class MenuServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * Indicates if loading of the provider is deferred.
20
     *
21
     * @var bool
22
     */
23
    protected $defer = false;
24
25
    /**
26
     * Register the service provider.
27
     *
28
     * @return void
29
     */
30
    public function register()
31
    {
32
        $this->registerBindings();
33
    }
34
35
    /**
36
     * Register all online menus on the Pingpong/Menu package
37
     */
38
    public function boot()
39
    {
40
        $this->registerMenus();
41
    }
42
43
    /**
44
     * Get the services provided by the provider.
45
     *
46
     * @return array
47
     */
48
    public function provides()
49
    {
50
        return array();
51
    }
52
53
    /**
54
     * Register class binding
55
     */
56
    private function registerBindings()
57
    {
58
        $this->app->bind(
59
            'Modules\Menu\Repositories\MenuRepository',
60
            function () {
61
                $repository = new EloquentMenuRepository(new Menu());
0 ignored issues
show
Documentation introduced by
new \Modules\Menu\Entities\Menu() is of type object<Modules\Menu\Entities\Menu>, but the function expects a object<Modules\Core\Repositories\Eloquent\Model>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
63
                if (! config('app.cache')) {
64
                    return $repository;
65
                }
66
67
                return new CacheMenuDecorator($repository);
68
            }
69
        );
70
71
        $this->app->bind(
72
            'Modules\Menu\Repositories\MenuItemRepository',
73
            function () {
74
                $repository = new EloquentMenuItemRepository(new Menuitem());
0 ignored issues
show
Documentation introduced by
new \Modules\Menu\Entities\Menuitem() is of type object<Modules\Menu\Entities\Menuitem>, but the function expects a object<Modules\Core\Repositories\Eloquent\Model>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
76
                if (! config('app.cache')) {
77
                    return $repository;
78
                }
79
80
                return new CacheMenuItemDecorator($repository);
81
            }
82
        );
83
    }
84
85
    /**
86
     * Add a menu item to the menu
87
     * @param Menuitem $item
88
     * @param Builder $menu
89
     */
90
    public function addItemToMenu(Menuitem $item, Builder $menu)
91
    {
92
        if ($this->hasChildren($item)) {
93
            $this->addChildrenToMenu($item->title, $item->items, $menu, ['icon' => $item->icon, 'target' => $item->target]);
0 ignored issues
show
Documentation introduced by
The property title 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...
Documentation introduced by
The property items 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...
Documentation introduced by
The property icon 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...
Documentation introduced by
The property target 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...
94 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
95
            $target = $this->getTarget($item);
96
            $menu->url(
97
                $target,
98
                $item->title,
0 ignored issues
show
Documentation introduced by
The property title 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...
99
                ['target' => $item->target,
0 ignored issues
show
Documentation introduced by
The property target 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...
100
                    'icon' => $item->icon]
0 ignored issues
show
Documentation introduced by
The property icon 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...
101
            );
102
        }
103
    }
104
105
    /**
106
     * Add children to menu under the give name
107
     *
108
     * @param string $name
109
     * @param object $children
110
     * @param Builder|MenuItem $menu
111
     */
112
    private function addChildrenToMenu($name, $children, $menu, $attribs = [])
113
    {
114
        $menu->dropdown($name, function (PingpongMenuItem $subMenu) use ($children) {
115
            foreach ($children as $child) {
116
                $this->addSubItemToMenu($child, $subMenu);
117
            }
118
        }, 0, $attribs);
119
    }
120
121
    /**
122
     * Add children to the given menu recursively
123
     * @param Menuitem $child
124
     * @param PingpongMenuItem $sub
125
     */
126
    private function addSubItemToMenu(Menuitem $child, PingpongMenuItem $sub)
127
    {
128
        if ($this->hasChildren($child)) {
129
            $this->addChildrenToMenu($child->title, $child->items, $sub);
0 ignored issues
show
Documentation introduced by
The property title 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...
Documentation introduced by
The property items 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...
Documentation introduced by
$sub is of type object<Pingpong\Menus\MenuItem>, but the function expects a object<Pingpong\Menus\Me...enu\Providers\MenuItem>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
131
            $target = $this->getTarget($child);
132
            $sub->url($target, $child->title, 0, ['icon' => $child->icon, 'target' => $target]);
0 ignored issues
show
Documentation introduced by
The property title 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...
Documentation introduced by
The property icon 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...
133
        }
134
    }
135
136
    /**
137
     * Check if the given menu item has children
138
     *
139
     * @param  object $item
140
     * @return bool
141
     */
142
    private function hasChildren($item)
143
    {
144
        return $item->items->count() > 0;
145
    }
146
147
    /**
148
     * Register the active menus
149
     */
150
    private function registerMenus()
151
    {
152
        if (! $this->app['asgard.isInstalled']) {
153
            return;
154
        }
155
        $menu = $this->app->make('Modules\Menu\Repositories\MenuRepository');
156
        $menuItem = $this->app->make('Modules\Menu\Repositories\MenuItemRepository');
157
        foreach ($menu->allOnline() as $menu) {
158
            $menuTree = $menuItem->getTreeForMenu($menu->id);
159
            MenuFacade::create($menu->name, function (Builder $menu) use ($menuTree) {
0 ignored issues
show
Bug introduced by
The method create() does not exist on Pingpong\Menus\MenuFacade. Did you maybe mean createFreshMockInstance()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
160
                foreach ($menuTree as $menuItem) {
161
                    $this->addItemToMenu($menuItem, $menu);
162
                }
163
            });
164
        }
165
    }
166
167
    /**
168
     * Get link target
169
     *
170
     * @param $item
171
     * @return mixed
172
     */
173
    private function getTarget($item)
174
    {
175
        if (empty($item->url)) {
176
            $linkPathArray = array();
177
            $parentItem = $item;
178
179
            $hasParentItem = !is_null($item->parent_id) ? true : false;
180
181
            while ($hasParentItem) {
182
                $parentItem = Menuitem::where('id', '=', $parentItem->parent_id)->first();
183
184
                if ($parentItem->is_root != true) {
185
                    if (!empty($parentItem->page_id)) {
186
                        $page = Page::where('id', '=', $parentItem->page_id)->first()->translate($this->app->getLocale());
187
                        array_push($linkPathArray, $page->slug);
188
                    } else {
189
                        $parentUri = !is_null($parentItem->uri) ? $parentItem->uri . '/' . $linkPathArray : $linkPathArray;
190
                        array_push($linkPathArray, $parentUri);
191
                    }
192
                }
193
                $hasParentItem = !is_null($parentItem->parent_id) ? true : false;
194
            }
195
196
            $linkPathArray = array_reverse($linkPathArray);
197
            if (!empty($item->page_id)) {
198
                $page = Page::where('id', '=', $item->page_id)->first()->translate($this->app->getLocale());
199
200
                array_push($linkPathArray, $page->slug);
201
            } else {
202
                array_push($linkPathArray, $item->uri);
203
            }
204
            $parentLinkPath = implode('/', $linkPathArray);
205
        } else {
206
            $parentLinkPath = $item->url;
207
        }
208
209
        return $parentLinkPath;
210
    }
211
212
    /**
213
     * Get parent item
214
     *
215
     * @param $item
216
     * @return mixed
217
     */
218
    /*private function getParentItem($item){
219
       // \Debugbar::info($item->parent_id);
220
        //Menuitem::where('id','=',$item->parent_id);
221
222
        return $target;
223
    }*/
224
}
225