Completed
Push — master ( 8b8176...86815f )
by wen
03:16
created

Component::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Sco\Admin\Component;
4
5
use Illuminate\Foundation\Application;
6
use KodiComponents\Navigation\Contracts\BadgeInterface;
7
use Sco\Admin\Component\Concerns\HasEvents;
8
use Sco\Admin\Component\Concerns\HasPermission;
9
use Sco\Admin\Contracts\ComponentInterface;
10
use Sco\Admin\Contracts\RepositoryInterface;
11
use Sco\Admin\Navigation\Badge;
12
use Sco\Admin\Navigation\Page;
13
14
abstract class Component implements ComponentInterface
15
{
16
    use HasEvents, HasPermission;
17
18
    protected $name;
19
20
    protected $app;
21
22
    protected $title;
23
24
    protected $repository;
25
26
    protected $model;
27
28
    protected static $booted = [];
29
30
    /**
31
     * @var \Illuminate\Contracts\Events\Dispatcher
32
     */
33
    protected static $dispatcher;
34
35
    public function __construct(Application $app, $modelClass)
36
    {
37
        $this->app = $app;
38
39
        $this->repository = $this->app->make(RepositoryInterface::class);
40
        $this->repository->setClass($modelClass);
41
42
        $this->model = $this->repository->getModel();
43
        if (!$this->name) {
44
            $this->setDefaultName();
45
        }
46
47
        $this->bootIfNotBooted();
48
    }
49
50
    protected function setDefaultName()
51
    {
52
        $this->name = $this->getModelClassName();
53
    }
54
55
    protected function getModelClassName()
56
    {
57
        return snake_case(str_plural(class_basename(get_class($this->getModel()))));
58
    }
59
60
    public function getName()
61
    {
62
        return $this->name;
63
    }
64
65
    public function getTitle()
66
    {
67
        return $this->title;
68
    }
69
70
    public function getModel()
71
    {
72
        return $this->model;
73
    }
74
75
    public function getRepository()
76
    {
77
        return $this->repository;
78
    }
79
80
    public function get()
81
    {
82
        if (!method_exists($this, 'callView')) {
83
            return;
84
        }
85
86
        $view = $this->app->call([$this, 'callView']);
0 ignored issues
show
Unused Code introduced by
$view is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
87
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getConfigs()
94
    {
95
        return collect([
96
            'primaryKey'  => $this->getModel()->getKeyName(),
97
            'title'       => $this->getTitle(),
98
            'permissions' => $this->getPermissions(),
99
            //'columns'     => $this->getColumns()->values(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
100
            //'elements'    => $this->getElements()->values(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
        ]);
102
    }
103
104
    /**
105
     * @return \KodiComponents\Navigation\Contracts\NavigationInterface
106
     */
107
    public function getNavigation()
108
    {
109
        return $this->app['admin.navigation'];
110
    }
111
112
    /**
113
     * 添加菜单
114
     *
115
     * @param int  $priority
116
     * @param null $badge
117
     *
118
     * @return \Sco\Admin\Navigation\Page
119
     */
120
    public function addToNavigation($priority = 100, $badge = null)
121
    {
122
        $page = $this->makePage($priority, $badge);
123
        $this->getNavigation()->addPage($page);
124
        return $page;
125
    }
126
127
    /**
128
     * page
129
     *
130
     * @param int  $priority
131
     * @param null $badge
132
     *
133
     * @return \Sco\Admin\Navigation\Page
134
     */
135
    protected function makePage($priority = 100, $badge = null)
136
    {
137
        $page = new Page($this->getTitle());
138
        $page->setPriority($priority);
139
        if ($badge) {
140
            if (!($badge instanceof BadgeInterface)) {
141
                $badge = new Badge($badge);
142
            }
143
            $page->addBadge($badge);
144
        }
145
146
        return $page;
147
    }
148
149
    protected function bootIfNotBooted()
150
    {
151
        if (!isset(static::$booted[static::class])) {
152
            static::$booted[static::class] = true;
153
154
            $this->fireEvent('booting', false);
155
156
            $this->boot();
0 ignored issues
show
Unused Code introduced by
The call to the method Sco\Admin\Component\Component::boot() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
157
158
            $this->fireEvent('booted', false);
159
        }
160
    }
161
162
    public function boot()
163
    {
164
        return true;
165
    }
166
}
167