Completed
Push — master ( 5e765d...c5065d )
by wen
03:03
created

ModelConfig::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

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 1
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 Illuminate\Http\Request;
8
use JsonSerializable;
9
use Illuminate\Contracts\Support\Arrayable;
10
use Illuminate\Contracts\Support\Jsonable;
11
use Sco\Admin\Contracts\Config as ConfigContract;
12
use Sco\Admin\Contracts\Repository as RepositoryContract;
13
use Sco\Attributes\HasAttributesTrait;
14
15
class ModelConfig implements Arrayable, Jsonable, JsonSerializable
16
{
17
    use HasAttributesTrait;
18
19
    /**
20
     * @var \Illuminate\Foundation\Application
21
     */
22
    protected $app;
23
24
    /**
25
     * @var \Sco\Admin\Contracts\Config
26
     */
27
    protected $configFactory;
28
    /**
29
     * @var \Illuminate\Database\Eloquent\Model
30
     */
31
    //protected $model;
32
33
    /**
34
     * @var mixed|\Sco\Admin\Repositories\Repository
35
     */
36
    protected $repository;
37
38
    protected $config;
39
40
    public function __construct(Application $app, ConfigContract $factory)
41
    {
42
        $this->app = $app;
43
        $this->configFactory = $factory;
44
        //$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...
45
        $this->config = new ConfigRepository(
46
            $this->getConfigValues()
47
        );
48
49
        $this->repository = $this->app->make(RepositoryContract::class);
50
51
        $this->repository->setClass(
52
            $this->config->get('class')
53
        );
54
    }
55
56
    /**
57
     * @return mixed|\Sco\Admin\Repositories\Repository
58
     */
59
    public function getRepository()
60
    {
61
        return $this->repository;
62
    }
63
64
    public function get()
65
    {
66
        $repository = $this->getRepository();
67
        $orderBy = $this->config->get('orderBy', [$repository->getKeyName(), 'desc']);
68
        $query = $repository->orderBy($orderBy[0], $orderBy[1]);
69
70
        if ($repository->isRestorable()) {
71
            $query = $query->withTrashed();
72
        }
73
74
        if ($this->usePagination()) {
75
            $data = $query->paginate($this->config->get('perPage'));
76
77
            $data->setCollection($this->parseRows($data->items()));
78
        } else {
79
            $data = $this->parseRows($query->get());
80
        }
81
82
        return $data;
83
    }
84
85
    public function store(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
    {
87
88
    }
89
90
    public function delete($id)
91
    {
92
        $this->getRepository()->findOrFail($id)->delete();
93
        return true;
94
    }
95
96
    public function forceDelete($id)
97
    {
98
        $this->getRepository()->forceDelete($id);
99
        return true;
100
    }
101
102
    public function restore($id)
103
    {
104
        $this->getRepository()->restore($id);
105
    }
106
107
    protected function parseRows($rows)
108
    {
109
        $data = collect();
110
        if ($rows) {
111
            foreach ($rows as $row) {
112
                $newRow = collect();
113
                foreach ($this->configFactory->getColumns() as $column) {
114
                    $newRow->put($column->getName(), $column->setModel($row)->render());
115
116
                    // whether this row has been soft deleted
117
                    if ($this->getRepository()->isRestorable()) {
118
                        $newRow->put('_deleted', $row->trashed() ? 1 : 0);
119
                    }
120
                }
121
                $data->push($newRow);
122
            }
123
        }
124
        return $data;
125
    }
126
127
    protected function usePagination()
128
    {
129
        return $this->config->get('perPage') > 0;
130
    }
131
132
    protected function getConfigValues()
133
    {
134
        $config = $this->configFactory->getConfigRepository()->get('model');
135
        if (is_string($config)) {
136
            $config = [
137
                'class' => $config
138
            ];
139
        }
140
        $config = array_merge([
141
            //'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...
142
            'perPage' => 10,
143
        ], $config);
144
        return $config;
145
    }
146
147
    /**
148
     * Handle dynamic method calls into the model.
149
     *
150
     * @param  string  $method
151
     * @param  array  $parameters
152
     * @return mixed
153
     */
154
    /*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...
155
    {
156
        if (in_array($method, ['getKeyName'])) {
157
            return $this->model->$method(...$parameters);
158
        }
159
160
        $this->model = $this->model->$method(...$parameters);
161
        return $this;
162
    }*/
163
164
    /**
165
     * Handle dynamic static method calls into the method.
166
     *
167
     * @param  string  $method
168
     * @param  array  $parameters
169
     * @return mixed
170
     */
171
    /*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...
172
    {
173
        return (new static)->$method(...$parameters);
174
    }*/
175
}
176