Completed
Push — master ( 86815f...7e02e6 )
by wen
02:47
created

Component::getColumns()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 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\HasPermission;
8
use Sco\Admin\Contracts\ComponentInterface;
9
use Sco\Admin\Contracts\RepositoryInterface;
10
11
abstract class Component implements ComponentInterface
12
{
13
    use HasEvents, HasPermission;
14
15
    /**
16
     * @var
17
     */
18
    protected $name;
19
20
    /**
21
     * @var \Illuminate\Foundation\Application
22
     */
23
    protected $app;
24
25
    protected $title;
26
27
    /**
28
     * @var mixed|\Sco\Admin\Contracts\RepositoryInterface
29
     */
30
    protected $repository;
31
32
    /**
33
     * @var \Illuminate\Database\Eloquent\Model
34
     */
35
    protected $model;
36
37
    protected static $booted = [];
38
39
    /**
40
     * @var \Illuminate\Contracts\Events\Dispatcher
41
     */
42
    protected static $dispatcher;
43
44
    /**
45
     * @return array
46
     */
47
    abstract public function getColumns();
48
49
    public function __construct(Application $app, $modelClass)
50
    {
51
        $this->app = $app;
52
53
        $this->repository = $this->app->make(RepositoryInterface::class);
54
        $this->repository->setClass($modelClass);
55
56
        $this->model = $this->repository->getModel();
57
        if (!$this->name) {
58
            $this->setDefaultName();
59
        }
60
61
        $this->registerObserver($this->permissionObserver);
62
63
        $this->bootIfNotBooted();
64
    }
65
66
    protected function setDefaultName()
67
    {
68
        $this->name = $this->getModelClassName();
69
    }
70
71
    protected function getModelClassName()
72
    {
73
        return snake_case(str_plural(class_basename(get_class($this->getModel()))));
74
    }
75
76
    public function getName()
77
    {
78
        return $this->name;
79
    }
80
81
    public function getTitle()
82
    {
83
        return $this->title;
84
    }
85
86
    public function getModel()
87
    {
88
        return $this->model;
89
    }
90
91
    public function getRepository()
92
    {
93
        return $this->repository;
94
    }
95
96
    public function get()
97
    {
98
        if (!method_exists($this, 'callView')) {
99
            return;
100
        }
101
102
        $view = $this->app->call([$this, 'callView']);
103
        $view->setRepository($this->getRepository());
104
105
        return $view->get();
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getConfigs()
112
    {
113
        return collect([
114
            'primaryKey'  => $this->getModel()->getKeyName(),
115
            'title'       => $this->getTitle(),
116
            'permissions' => $this->getPermissions(),
117
            'columns'     => $this->getColumns(),
118
            //'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...
119
        ]);
120
    }
121
122
    protected function bootIfNotBooted()
123
    {
124
        if (!isset(static::$booted[static::class])) {
125
            static::$booted[static::class] = true;
126
127
            $this->fireEvent('booting', false);
128
129
            $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...
130
131
            $this->fireEvent('booted', false);
132
        }
133
    }
134
135
    public function boot()
136
    {
137
        return true;
138
    }
139
}
140