Completed
Push — develop ( c125f5...ef8688 )
by Abdelrahman
01:25
created

TenantsController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Tenants\Http\Controllers\Adminarea;
6
7
use Cortex\Tenants\Models\Tenant;
8
use Illuminate\Foundation\Http\FormRequest;
9
use Cortex\Foundation\DataTables\LogsDataTable;
10
use Cortex\Foundation\Importers\DefaultImporter;
11
use Cortex\Foundation\DataTables\ImportLogsDataTable;
12
use Cortex\Foundation\Http\Requests\ImportFormRequest;
13
use Cortex\Tenants\DataTables\Adminarea\TenantsDataTable;
14
use Cortex\Foundation\Http\Controllers\AuthorizedController;
15
use Cortex\Tenants\Http\Requests\Adminarea\TenantFormRequest;
16
17
class TenantsController extends AuthorizedController
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected $resource = Tenant::class;
23
24
    /**
25
     * List all tenants.
26
     *
27
     * @param \Cortex\Tenants\DataTables\Adminarea\TenantsDataTable $tenantsDataTable
28
     *
29
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
30
     */
31
    public function index(TenantsDataTable $tenantsDataTable)
32
    {
33
        return $tenantsDataTable->with([
34
            'id' => 'adminarea-tenants-index-table',
35
            'phrase' => trans('cortex/tenants::common.tenants'),
36
        ])->render('cortex/foundation::adminarea.pages.datatable');
37
    }
38
39
    /**
40
     * List tenant logs.
41
     *
42
     * @param \Cortex\Tenants\Models\Tenant               $tenant
43
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
44
     *
45
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonRes...e|\Illuminate\View\View?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
46
     */
47
    public function logs(Tenant $tenant, LogsDataTable $logsDataTable)
48
    {
49
        return $logsDataTable->with([
50
            'resource' => $tenant,
51
            'tabs' => 'adminarea.tenants.tabs',
52
            'phrase' => trans('cortex/tenants::common.tenants'),
53
            'id' => "adminarea-tenants-{$tenant->getKey()}-logs-table",
54
        ])->render('cortex/foundation::adminarea.pages.datatable-logs');
55
    }
56
57
    /**
58
     * Import tenants.
59
     *
60
     * @return \Illuminate\View\View
61
     */
62
    public function import()
63
    {
64
        return view('cortex/foundation::adminarea.pages.import', [
65
            'id' => 'adminarea-tenants-import',
66
            'tabs' => 'adminarea.tenants.tabs',
67
            'url' => route('adminarea.tenants.hoard'),
68
            'phrase' => trans('cortex/tenants::common.tenants'),
69
        ]);
70
    }
71
72
    /**
73
     * Hoard tenants.
74
     *
75
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
76
     * @param \Cortex\Foundation\Importers\DefaultImporter       $importer
77
     *
78
     * @return void
79
     */
80
    public function hoard(ImportFormRequest $request, DefaultImporter $importer)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
81
    {
82
        // Handle the import
83
        $importer->config['resource'] = $this->resource;
84
        $importer->handleImport();
85
    }
86
87
    /**
88
     * List tenant import logs.
89
     *
90
     * @param \Cortex\Foundation\DataTables\ImportLogsDataTable $importLogsDatatable
91
     *
92
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonRes...e|\Illuminate\View\View?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
93
     */
94
    public function importLogs(ImportLogsDataTable $importLogsDatatable)
95
    {
96
        return $importLogsDatatable->with([
97
            'resource' => 'tenant',
98
            'tabs' => 'adminarea.tenants.tabs',
99
            'id' => 'adminarea-tenants-import-logs-table',
100
            'phrase' => trans('cortex/tenants::common.tenants'),
101
        ])->render('cortex/foundation::adminarea.pages.datatable-import-logs');
102
    }
103
104
    /**
105
     * Create new tenant.
106
     *
107
     * @param \Cortex\Tenants\Models\Tenant $tenant
108
     *
109
     * @return \Illuminate\View\View
110
     */
111
    public function create(Tenant $tenant)
112
    {
113
        return $this->form($tenant);
114
    }
115
116
    /**
117
     * Edit given tenant.
118
     *
119
     * @param \Cortex\Tenants\Models\Tenant $tenant
120
     *
121
     * @return \Illuminate\View\View
122
     */
123
    public function edit(Tenant $tenant)
124
    {
125
        return $this->form($tenant);
126
    }
127
128
    /**
129
     * Show tenant create/edit form.
130
     *
131
     * @param \Cortex\Tenants\Models\Tenant $tenant
132
     *
133
     * @return \Illuminate\View\View
134
     */
135
    protected function form(Tenant $tenant)
136
    {
137
        $countries = collect(countries())->map(function ($country, $code) {
138
            return [
139
                'id' => $code,
140
                'text' => $country['name'],
141
                'emoji' => $country['emoji'],
142
            ];
143
        })->values();
144
        $languages = collect(languages())->pluck('name', 'iso_639_1');
145
        $owners = app('cortex.auth.manager')->all()->pluck('username', 'id');
146
        $groups = app('rinvex.tenants.tenant')->distinct()->get(['group'])->pluck('group', 'group')->toArray();
147
148
        return view('cortex/tenants::adminarea.pages.tenant', compact('tenant', 'owners', 'countries', 'languages', 'groups'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
149
    }
150
151
    /**
152
     * Store new tenant.
153
     *
154
     * @param \Cortex\Tenants\Http\Requests\Adminarea\TenantFormRequest $request
155
     * @param \Cortex\Tenants\Models\Tenant                             $tenant
156
     *
157
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
158
     */
159
    public function store(TenantFormRequest $request, Tenant $tenant)
160
    {
161
        return $this->process($request, $tenant);
162
    }
163
164
    /**
165
     * Update given tenant.
166
     *
167
     * @param \Cortex\Tenants\Http\Requests\Adminarea\TenantFormRequest $request
168
     * @param \Cortex\Tenants\Models\Tenant                             $tenant
169
     *
170
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
171
     */
172
    public function update(TenantFormRequest $request, Tenant $tenant)
173
    {
174
        return $this->process($request, $tenant);
175
    }
176
177
    /**
178
     * Process stored/updated tenant.
179
     *
180
     * @param \Illuminate\Foundation\Http\FormRequest $request
181
     * @param \Cortex\Tenants\Models\Tenant           $tenant
182
     *
183
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
184
     */
185
    protected function process(FormRequest $request, Tenant $tenant)
186
    {
187
        // Prepare required input fields
188
        $data = $request->validated();
189
190
        ! $request->hasFile('profile_picture')
191
        || $tenant->addMediaFromRequest('profile_picture')
192
                       ->sanitizingFileName(function ($fileName) {
193
                           return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
194
                       })
195
                       ->toMediaCollection('profile_picture', config('cortex.auth.media.disk'));
196
197
        ! $request->hasFile('cover_photo')
198
        || $tenant->addMediaFromRequest('cover_photo')
199
                       ->sanitizingFileName(function ($fileName) {
200
                           return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
201
                       })
202
                       ->toMediaCollection('cover_photo', config('cortex.auth.media.disk'));
203
204
        // Save tenant
205
        $tenant->fill($data)->save();
206
        $tenant->owner->assign('owner');
207
        $tenant->owner->attachTenants($tenant);
208
209
        return intend([
210
            'url' => route('adminarea.tenants.index'),
211
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => 'tenant', 'id' => $tenant->name])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
212
        ]);
213
    }
214
215
    /**
216
     * Destroy given tenant.
217
     *
218
     * @param \Cortex\Tenants\Models\Tenant $tenant
219
     *
220
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
221
     */
222
    public function destroy(Tenant $tenant)
223
    {
224
        $tenant->delete();
225
226
        return intend([
227
            'url' => route('adminarea.tenants.index'),
228
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => 'tenant', 'id' => $tenant->name])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 140 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
229
        ]);
230
    }
231
}
232