Completed
Push — master ( 46aff2...b271b8 )
by wen
02:45
created

ModelConfig::parseRows()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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