Completed
Push — master ( b271b8...ef0682 )
by wen
03:13
created

ModelConfig::restore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Sco\Admin\Config;
4
5
use Illuminate\Config\Repository as ConfigRepository;
6
use Illuminate\Foundation\Application;
7
use JsonSerializable;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Contracts\Support\Jsonable;
10
use Sco\Admin\Contracts\Config as ConfigContract;
11
use Sco\Admin\Contracts\Repository as RepositoryContract;
12
use Sco\Attributes\HasAttributesTrait;
13
14
class ModelConfig implements Arrayable, Jsonable, JsonSerializable
15
{
16
    use HasAttributesTrait;
17
18
    /**
19
     * @var \Illuminate\Foundation\Application
20
     */
21
    protected $app;
22
23
    /**
24
     * @var \Sco\Admin\Contracts\Config
25
     */
26
    protected $configFactory;
27
    /**
28
     * @var \Illuminate\Database\Eloquent\Model
29
     */
30
    //protected $model;
31
32
    /**
33
     * @var mixed|\Sco\Admin\Repositories\Repository
34
     */
35
    protected $repository;
36
37
    protected $config;
38
39
    public function __construct(Application $app, ConfigContract $factory)
40
    {
41
        $this->app = $app;
42
        $this->configFactory = $factory;
43
        //$this->model = $model;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
44
        $this->config = new ConfigRepository(
45
            $this->getConfigValues()
46
        );
47
48
        $this->repository = $this->app->make(RepositoryContract::class);
49
50
        $this->repository->setClass(
51
            $this->config->get('class')
52
        );
53
    }
54
55
    /**
56
     * @return mixed|\Sco\Admin\Repositories\Repository
57
     */
58
    public function getRepository()
59
    {
60
        return $this->repository;
61
    }
62
63
    public function get()
64
    {
65
        $repository = $this->getRepository();
66
        $orderBy = $this->config->get('orderBy', [$repository->getKeyName(), 'desc']);
67
        $query = $repository->orderBy($orderBy[0], $orderBy[1]);
68
69
        if ($repository->isRestorable()) {
70
            $query = $query->withTrashed();
71
        }
72
73
        if ($this->usePagination()) {
74
            $data = $query->paginate($this->config->get('perPage'));
75
76
            $data->setCollection($this->parseRows($data->items()));
77
        } else {
78
            $data = $this->parseRows($query->get());
79
        }
80
81
        return $data;
82
    }
83
84
    public function delete($id)
85
    {
86
        $this->getRepository()->findOrFail($id)->delete();
87
        return true;
88
    }
89
90
    public function forceDelete($id)
91
    {
92
        $this->getRepository()->forceDelete($id);
93
        return true;
94
    }
95
96
    public function restore($id)
97
    {
98
        $this->getRepository()->restore($id);
99
    }
100
101
    protected function parseRows($rows)
102
    {
103
        $data = collect();
104
        if ($rows) {
105
            foreach ($rows as $row) {
106
                $newRow = collect();
107
                foreach ($this->configFactory->getColumns() as $column) {
108
                    $newRow->put($column->getName(), $column->setModel($row)->render());
109
110
                    // whether this row has been soft deleted
111
                    if ($this->getRepository()->isRestorable()) {
112
                        $newRow->put('_deleted', $row->trashed() ? 1 : 0);
113
                    }
114
                }
115
                $data->push($newRow);
116
            }
117
        }
118
        return $data;
119
    }
120
121
    protected function usePagination()
122
    {
123
        return $this->config->get('perPage') > 0;
124
    }
125
126
    protected function getConfigValues()
127
    {
128
        $config = $this->configFactory->getConfigRepository()->get('model');
129
        if (is_string($config)) {
130
            $config = [
131
                'class' => $config
132
            ];
133
        }
134
        $config = array_merge([
135
            //'orderBy' => [],
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...
136
            'perPage' => 10,
137
        ], $config);
138
        return $config;
139
    }
140
141
    /**
142
     * Handle dynamic method calls into the model.
143
     *
144
     * @param  string  $method
145
     * @param  array  $parameters
146
     * @return mixed
147
     */
148
    /*public function __call($method, $parameters)
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% 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...
149
    {
150
        if (in_array($method, ['getKeyName'])) {
151
            return $this->model->$method(...$parameters);
152
        }
153
154
        $this->model = $this->model->$method(...$parameters);
155
        return $this;
156
    }*/
157
158
    /**
159
     * Handle dynamic static method calls into the method.
160
     *
161
     * @param  string  $method
162
     * @param  array  $parameters
163
     * @return mixed
164
     */
165
    /*public static function __callStatic($method, $parameters)
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
166
    {
167
        return (new static)->$method(...$parameters);
168
    }*/
169
}
170