Completed
Push — master ( 904ceb...736408 )
by wen
02:43
created

ModelConfig::delete()   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 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
    protected function parseRows($rows)
64
    {
65
        $data = collect();
66
        if ($rows) {
67
            foreach ($rows as $row) {
68
                $newRow = collect();
69
                foreach ($this->configFactory->getColumns() as $column) {
70
                    $newRow->put($column->getName(), $column->setModel($row)->render());
71
                }
72
                $data->push($newRow);
73
            }
74
        }
75
        return $data;
76
    }
77
78
    public function get()
79
    {
80
        $orderBy = $this->config->get('orderBy', [$this->getRepository()->getKeyName(), 'desc']);
81
        $query = $this->getRepository()->orderBy($orderBy[0], $orderBy[1]);
82
83
        if ($this->usePagination()) {
84
            $data = $query->paginate($this->config->get('perPage'));
85
86
            $data->setCollection($this->parseRows($data->items()));
87
        } else {
88
            $data = $this->parseRows($query->get());
89
        }
90
91
92
        return $data;
93
    }
94
95
    public function delete($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
96
    {
97
98
    }
99
100
    protected function usePagination()
101
    {
102
        return $this->config->get('perPage') > 0;
103
    }
104
105
    protected function getConfigValues()
106
    {
107
        $config = $this->configFactory->getConfigRepository()->get('model');
108
        if (is_string($config)) {
109
            $config = [
110
                'class' => $config
111
            ];
112
        }
113
        $config = array_merge([
114
            //'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...
115
            'perPage' => 10,
116
        ], $config);
117
        return $config;
118
    }
119
120
    /**
121
     * Handle dynamic method calls into the model.
122
     *
123
     * @param  string  $method
124
     * @param  array  $parameters
125
     * @return mixed
126
     */
127
    /*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...
128
    {
129
        if (in_array($method, ['getKeyName'])) {
130
            return $this->model->$method(...$parameters);
131
        }
132
133
        $this->model = $this->model->$method(...$parameters);
134
        return $this;
135
    }*/
136
137
    /**
138
     * Handle dynamic static method calls into the method.
139
     *
140
     * @param  string  $method
141
     * @param  array  $parameters
142
     * @return mixed
143
     */
144
    /*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...
145
    {
146
        return (new static)->$method(...$parameters);
147
    }*/
148
}
149