Completed
Push — master ( 7e02e6...ea0e93 )
by wen
03:01
created

Component   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 7
dl 0
loc 139
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A setDefaultName() 0 4 1
A getModelClassName() 0 4 1
A getName() 0 4 1
A getTitle() 0 4 1
A getModel() 0 4 1
A getRepository() 0 4 1
A get() 0 10 1
A getConfigs() 0 10 1
A fireView() 0 9 2
A bootIfNotBooted() 0 12 2
A boot() 0 4 1
1
<?php
2
3
namespace Sco\Admin\Component;
4
5
use Illuminate\Foundation\Application;
6
use Sco\Admin\Component\Concerns\HasEvents;
7
use Sco\Admin\Component\Concerns\HasNavigation;
8
use Sco\Admin\Component\Concerns\HasPermission;
9
use Sco\Admin\Contracts\ComponentInterface;
10
use Sco\Admin\Contracts\RepositoryInterface;
11
use Sco\Admin\Exceptions\BadMethodCallException;
12
13
abstract class Component implements ComponentInterface
14
{
15
    use HasEvents,
16
        HasPermission,
17
        HasNavigation;
18
19
    /**
20
     * @var
21
     */
22
    protected $name;
23
24
    /**
25
     * @var \Illuminate\Foundation\Application
26
     */
27
    protected $app;
28
29
    protected $title;
30
31
    /**
32
     * @var mixed|\Sco\Admin\Contracts\RepositoryInterface
33
     */
34
    protected $repository;
35
36
    /**
37
     * @var \Illuminate\Database\Eloquent\Model
38
     */
39
    protected $model;
40
41
    protected static $booted = [];
42
43
    /**
44
     * @var \Illuminate\Contracts\Events\Dispatcher
45
     */
46
    protected static $dispatcher;
47
48
    public function __construct(Application $app, $modelClass)
49
    {
50
        $this->app = $app;
51
52
        $this->repository = $this->app->make(RepositoryInterface::class);
53
        $this->repository->setClass($modelClass);
54
55
        $this->model = $this->repository->getModel();
56
        if (!$this->name) {
57
            $this->setDefaultName();
58
        }
59
60
        $this->registerObserver($this->permissionObserver);
61
62
        $this->bootIfNotBooted();
63
    }
64
65
    protected function setDefaultName()
66
    {
67
        $this->name = $this->getModelClassName();
68
    }
69
70
    protected function getModelClassName()
71
    {
72
        return snake_case(str_plural(class_basename(get_class($this->getModel()))));
73
    }
74
75
    public function getName()
76
    {
77
        return $this->name;
78
    }
79
80
    public function getTitle()
81
    {
82
        return $this->title;
83
    }
84
85
    public function getModel()
86
    {
87
        return $this->model;
88
    }
89
90
    public function getRepository()
91
    {
92
        return $this->repository;
93
    }
94
95
    public function get()
96
    {
97
        $view = $this->fireView();
98
99
        $this->getRepository();
0 ignored issues
show
Unused Code introduced by
The call to the method Sco\Admin\Component\Component::getRepository() 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...
100
101
        $view->setRepository($this->getRepository());
102
103
        return $view->get();
104
    }
105
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getConfigs()
111
    {
112
        return collect([
113
            'primaryKey'  => $this->getModel()->getKeyName(),
114
            'title'       => $this->getTitle(),
115
            'permissions' => $this->getPermissions(),
116
            'view'        => $this->fireView(),
117
            //'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...
118
        ]);
119
    }
120
121
    /**
122
     * @return \Sco\Admin\Contracts\ViewInterface
123
     */
124
    protected function fireView()
125
    {
126
        if (!method_exists($this, 'callView')) {
127
            throw new BadMethodCallException('Not Found Method "callView"');
128
        }
129
130
        $view = $this->app->call([$this, 'callView']);
131
        return $view;
132
    }
133
134
    protected function bootIfNotBooted()
135
    {
136
        if (!isset(static::$booted[static::class])) {
137
            static::$booted[static::class] = true;
138
139
            $this->fireEvent('booting', false);
140
141
            $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...
142
143
            $this->fireEvent('booted', false);
144
        }
145
    }
146
147
    public function boot()
148
    {
149
        return true;
150
    }
151
}
152