Completed
Push — master ( c5065d...1e6f86 )
by wen
02:30
created

ModelConfig::validate()   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 0
1
<?php
2
3
namespace Sco\Admin\Config;
4
5
use Illuminate\Config\Repository as ConfigRepository;
6
use Illuminate\Contracts\Validation\Factory;
7
use Illuminate\Foundation\Application;
8
use JsonSerializable;
9
use Illuminate\Contracts\Support\Arrayable;
10
use Illuminate\Contracts\Support\Jsonable;
11
use Sco\Admin\Contracts\ConfigFactoryInterface;
12
use Sco\Admin\Contracts\RepositoryInterface;
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, ConfigFactoryInterface $factory)
41
    {
42
        $this->app = $app;
43
        $this->configFactory = $factory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $factory of type object<Sco\Admin\Contrac...ConfigFactoryInterface> is incompatible with the declared type object<Sco\Admin\Contracts\Config> of property $configFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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(RepositoryInterface::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()
86
    {
87
        $this->validate();
88
89
    }
90
91
    public function delete($id)
92
    {
93
        $this->getRepository()->findOrFail($id)->delete();
94
        return true;
95
    }
96
97
    public function forceDelete($id)
98
    {
99
        $this->getRepository()->forceDelete($id);
100
        return true;
101
    }
102
103
    public function restore($id)
104
    {
105
        $this->getRepository()->restore($id);
106
    }
107
108
    protected function parseRows($rows)
109
    {
110
        $data = collect();
111
        if ($rows) {
112
            foreach ($rows as $row) {
113
                $newRow = collect();
114
                foreach ($this->configFactory->getColumns() as $column) {
115
                    $newRow->put($column->getName(), $column->setModel($row)->render());
116
117
                    // whether this row has been soft deleted
118
                    if ($this->getRepository()->isRestorable()) {
119
                        $newRow->put('_deleted', $row->trashed() ? 1 : 0);
120
                    }
121
                }
122
                $data->push($newRow);
123
            }
124
        }
125
        return $data;
126
    }
127
128
    protected function usePagination()
129
    {
130
        return $this->config->get('perPage') > 0;
131
    }
132
133
    protected function getConfigValues()
134
    {
135
        $config = $this->configFactory->getConfigRepository()->get('model');
136
        if (is_string($config)) {
137
            $config = [
138
                'class' => $config
139
            ];
140
        }
141
        $config = array_merge([
142
            //'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...
143
            'perPage' => 10,
144
        ], $config);
145
        return $config;
146
    }
147
148
    protected function validate()
149
    {
150
        $this->app->make(Factory::class)->validate($data, $rules, $messages, $customAttributes);
0 ignored issues
show
Bug introduced by
The variable $data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $rules does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $messages does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $customAttributes does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
151
    }
152
153
    /**
154
     * Handle dynamic method calls into the model.
155
     *
156
     * @param  string  $method
157
     * @param  array  $parameters
158
     * @return mixed
159
     */
160
    /*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...
161
    {
162
        if (in_array($method, ['getKeyName'])) {
163
            return $this->model->$method(...$parameters);
164
        }
165
166
        $this->model = $this->model->$method(...$parameters);
167
        return $this;
168
    }*/
169
170
    /**
171
     * Handle dynamic static method calls into the method.
172
     *
173
     * @param  string  $method
174
     * @param  array  $parameters
175
     * @return mixed
176
     */
177
    /*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...
178
    {
179
        return (new static)->$method(...$parameters);
180
    }*/
181
}
182