Completed
Push — master ( fda05d...111d7d )
by wen
03:17
created

ModelFactory::getConfigFactory()   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\ConfigManagerInterface;
13
use Sco\Admin\Contracts\ModelFactoryInterface;
14
use Sco\Admin\Contracts\RepositoryInterface;
15
16
class ModelFactory implements ModelFactoryInterface
17
{
18
    /**
19
     * @var \Illuminate\Foundation\Application
20
     */
21
    protected $app;
22
23
    /**
24
     * @var \Sco\Admin\Contracts\ConfigManagerInterface
25
     */
26
    protected $configManager;
27
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, ConfigManagerInterface $configManager)
41
    {
42
        $this->app = $app;
43
        $this->configManager = $configManager;
44
        $this->config = new ConfigRepository(
45
            $this->getConfigValues()
46
        );
47
48
        $this->repository = $this->app->make(RepositoryInterface::class);
49
50
        $this->repository->setClass(
51
            $this->config->get('class')
52
        );
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getRepository()
59
    {
60
        return $this->repository;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getConfigManager()
67
    {
68
        return $this->configManager;
69
    }
70
71
    public function get()
72
    {
73
        $repository = $this->getRepository();
74
        $orderBy = $this->config->get('orderBy', [$repository->getKeyName(), 'desc']);
75
        $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...
76
77
        if ($repository->isRestorable()) {
78
            $query = $query->withTrashed();
79
        }
80
81
        if ($this->usePagination()) {
82
            $data = $query->paginate($this->config->get('perPage'));
83
84
            $data->setCollection($this->parseRows($data->items()));
85
        } else {
86
            $data = $this->parseRows($query->get());
87
        }
88
89
        return $data;
90
    }
91
92
    public function store()
93
    {
94
        $this->validate();
95
    }
96
97
    public function update()
98
    {
99
    }
100
101
    public function delete($id)
102
    {
103
        $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...
104
        return true;
105
    }
106
107
    public function forceDelete($id)
108
    {
109
        $this->getRepository()->forceDelete($id);
110
        return true;
111
    }
112
113
    public function restore($id)
114
    {
115
        $this->getRepository()->restore($id);
116
    }
117
118
    protected function parseRows($rows)
119
    {
120
        if ($rows) {
121
            return $rows->map(function ($row) {
122
                $newRow = $this->configManager->getColumns()->mapWithKeys(function ($column) use ($row) {
123
                    return [$column->getName() => $column->setModel($row)->render()];
124
                });
125
126
                // whether this row has been soft deleted
127
                if ($this->getRepository()->isRestorable()) {
128
                    $newRow->put('_deleted', $row->trashed() ? 1 : 0);
129
                }
130
                return $newRow;
131
            });
132
        }
133
        return collect();
134
    }
135
136
    protected function usePagination()
137
    {
138
        return $this->config->get('perPage') > 0;
139
    }
140
141
    protected function getConfigValues()
142
    {
143
        $config = $this->configManager->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