Completed
Push — master ( 3701fc...fda05d )
by wen
03:03
created

ModelFactory::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\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\ModelFactoryInterface;
13
use Sco\Admin\Contracts\RepositoryInterface;
14
15
class ModelFactory implements ModelFactoryInterface
16
{
17
    /**
18
     * @var \Illuminate\Foundation\Application
19
     */
20
    protected $app;
21
22
    /**
23
     * @var \Sco\Admin\Contracts\Config
24
     */
25
    protected $configFactory;
26
    /**
27
     * @var \Illuminate\Database\Eloquent\Model
28
     */
29
    //protected $model;
30
31
    /**
32
     * @var mixed|\Sco\Admin\Repositories\Repository
33
     */
34
    protected $repository;
35
36
    protected $config;
37
38
    public function __construct(Application $app, ConfigFactoryInterface $factory)
39
    {
40
        $this->app = $app;
41
        $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...
42
        $this->config = new ConfigRepository(
43
            $this->getConfigValues()
44
        );
45
46
        $this->repository = $this->app->make(RepositoryInterface::class);
47
48
        $this->repository->setClass(
49
            $this->config->get('class')
50
        );
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getRepository()
57
    {
58
        return $this->repository;
59
    }
60
61
    /**
62
     * @return \Sco\Admin\Contracts\ConfigFactoryInterface
63
     */
64
    public function getConfigFactory()
65
    {
66
        return $this->configFactory;
67
    }
68
69
    public function get()
70
    {
71
        $repository = $this->getRepository();
72
        $orderBy = $this->config->get('orderBy', [$repository->getKeyName(), 'desc']);
73
        $query = $repository->orderBy($orderBy[0], $orderBy[1]);
0 ignored issues
show
Bug introduced by
The method orderBy() does not seem to exist on object<Sco\Admin\Contracts\RepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
75
        if ($repository->isRestorable()) {
76
            $query = $query->withTrashed();
77
        }
78
79
        if ($this->usePagination()) {
80
            $data = $query->paginate($this->config->get('perPage'));
81
82
            $data->setCollection($this->parseRows($data->items()));
83
        } else {
84
            $data = $this->parseRows($query->get());
85
        }
86
87
        return $data;
88
    }
89
90
    public function store()
91
    {
92
        $this->validate();
93
    }
94
95
    public function update()
96
    {
97
    }
98
99
    public function delete($id)
100
    {
101
        $this->getRepository()->findOrFail($id)->delete();
0 ignored issues
show
Bug introduced by
The method findOrFail() does not seem to exist on object<Sco\Admin\Contracts\RepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
        return true;
103
    }
104
105
    public function forceDelete($id)
106
    {
107
        $this->getRepository()->forceDelete($id);
108
        return true;
109
    }
110
111
    public function restore($id)
112
    {
113
        $this->getRepository()->restore($id);
114
    }
115
116
    protected function parseRows($rows)
117
    {
118
        $data = collect();
119
        if ($rows) {
120
            foreach ($rows as $row) {
121
                $newRow = collect();
122
                foreach ($this->configFactory->getColumns() as $column) {
123
                    $newRow->put($column->getName(), $column->setModel($row)->render());
124
125
                    // whether this row has been soft deleted
126
                    if ($this->getRepository()->isRestorable()) {
127
                        $newRow->put('_deleted', $row->trashed() ? 1 : 0);
128
                    }
129
                }
130
                $data->push($newRow);
131
            }
132
        }
133
        return $data;
134
    }
135
136
    protected function usePagination()
137
    {
138
        return $this->config->get('perPage') > 0;
139
    }
140
141
    protected function getConfigValues()
142
    {
143
        $config = $this->configFactory->getConfigRepository()->get('model');
144
        if (is_string($config)) {
145
            $config = [
146
                'class' => $config
147
            ];
148
        }
149
        $config = array_merge([
150
            //'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...
151
            'perPage' => 10,
152
        ], $config);
153
        return $config;
154
    }
155
156
    protected function validate()
157
    {
158
        $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...
159
    }
160
}
161