MenuPresenter::url()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 20
rs 9.2
1
<?php
2
3
namespace Yajra\CMS\Presenters;
4
5
use Laracasts\Presenter\Presenter;
6
use Yajra\CMS\Contracts\UrlGenerator;
7
8
class MenuPresenter extends Presenter
9
{
10
    /**
11
     * Link href target window.
12
     *
13
     * @return string
14
     */
15
    public function target()
16
    {
17
        return $this->entity->target == 0 ? '_self' : '_blank';
18
    }
19
20
    /**
21
     * Indented title against depth.
22
     *
23
     * @param int $start
24
     * @param string $symbol
25
     * @return string
26
     */
27
    public function indentedTitle($start = 1, $symbol = '— ')
28
    {
29
        $repeat = $this->entity->depth - $start;
30
31
        return str_repeat($symbol, $repeat >= 0 ? $repeat : 0) . ' ' . $this->entity->title;
32
    }
33
34
    /**
35
     * Generate menu url depending on type the menu.
36
     *
37
     * @return string
38
     */
39
    public function url()
40
    {
41
        /** @var \Yajra\CMS\Repositories\Extension\Repository $repository */
42
        $repository = app('extensions');
43
        /** @var \Yajra\CMS\Entities\Extension $extension */
44
        $extension = $repository->findOrFail($this->entity->extension_id);
45
        $class     = $extension->param('class');
46
        if (class_exists($class)) {
47
            $entity = app($class)->findOrNew($this->entity->param('id'));
48
            if ($entity instanceof UrlGenerator) {
49
                if (! $entity->exists) {
0 ignored issues
show
Bug introduced by
Accessing exists on the interface Yajra\CMS\Contracts\UrlGenerator suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
50
                    return '#';
51
                }
52
53
                return $entity->getUrl($this->entity->param('data'));
54
            }
55
        }
56
57
        return url($this->entity->url);
58
    }
59
60
    /**
61
     * Get menu link custom style.
62
     *
63
     * @return mixed
64
     */
65
    public function linkStyle()
66
    {
67
        return $this->entity->param('link_style');
68
    }
69
70
    /**
71
     * Get menu link title.
72
     *
73
     * @return mixed
74
     */
75
    public function linkTitle()
76
    {
77
        return $this->entity->param('link_title') ? $this->entity->param('link_title') : $this->entity->title;
78
    }
79
}
80