Completed
Push — develop ( d15310...a763d0 )
by Abdelrahman
11:20 queued 09:46
created

TenantsController::edit()   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
declare(strict_types=1);
4
5
namespace Cortex\Tenants\Http\Controllers\Adminarea;
6
7
use Rinvex\Tenants\Models\Tenant;
8
use Illuminate\Foundation\Http\FormRequest;
9
use Cortex\Foundation\DataTables\LogsDataTable;
10
use Cortex\Tenants\DataTables\Adminarea\TenantsDataTable;
11
use Cortex\Foundation\Http\Controllers\AuthorizedController;
12
use Cortex\Tenants\Http\Requests\Adminarea\TenantFormRequest;
13
14
class TenantsController extends AuthorizedController
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected $resource = 'tenant';
20
21
    /**
22
     * List all tenants.
23
     *
24
     * @param \Cortex\Tenants\DataTables\Adminarea\TenantsDataTable $tenantsDataTable
25
     *
26
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
27
     */
28
    public function index(TenantsDataTable $tenantsDataTable)
29
    {
30
        return $tenantsDataTable->with([
31
            'id' => 'adminarea-tenants-index-table',
32
            'phrase' => trans('cortex/tenants::common.tenants'),
33
        ])->render('cortex/foundation::adminarea.pages.datatable');
34
    }
35
36
    /**
37
     * List tenant logs.
38
     *
39
     * @param \Cortex\Tenants\Models\Tenant               $tenant
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $tenant a bit more specific; maybe use Tenant.
Loading history...
40
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
41
     *
42
     * @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...
43
     */
44
    public function logs(Tenant $tenant, LogsDataTable $logsDataTable)
45
    {
46
        return $logsDataTable->with([
47
            'resource' => $tenant,
48
            'tabs' => 'adminarea.tenants.tabs',
49
            'phrase' => trans('cortex/tenants::common.tenants'),
50
            'id' => "adminarea-tenants-{$tenant->getKey()}-logs-table",
51
        ])->render('cortex/foundation::adminarea.pages.datatable-logs');
52
    }
53
54
    /**
55
     * Create new tenant.
56
     *
57
     * @param \Cortex\Fort\Models\Role $tenant
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $tenant a bit more specific; maybe use Tenant.
Loading history...
58
     *
59
     * @return \Illuminate\View\View
60
     */
61
    public function create(Tenant $tenant)
62
    {
63
        return $this->form($tenant);
64
    }
65
66
    /**
67
     * Edit given tenant.
68
     *
69
     * @param \Cortex\Fort\Models\Role $tenant
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $tenant a bit more specific; maybe use Tenant.
Loading history...
70
     *
71
     * @return \Illuminate\View\View
72
     */
73
    public function edit(Tenant $tenant)
74
    {
75
        return $this->form($tenant);
76
    }
77
78
    /**
79
     * Show tenant create/edit form.
80
     *
81
     * @param \Cortex\Tenants\Models\Tenant $tenant
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $tenant a bit more specific; maybe use Tenant.
Loading history...
82
     *
83
     * @return \Illuminate\View\View
84
     */
85
    protected function form(Tenant $tenant)
86
    {
87
        $countries = collect(countries())->map(function ($country, $code) {
88
            return [
89
                'id' => $code,
90
                'text' => $country['name'],
91
                'emoji' => $country['emoji'],
92
            ];
93
        })->values();
94
        $languages = collect(languages())->pluck('name', 'iso_639_1');
95
        $owners = app('cortex.fort.user')->withAnyRoles(['manager'])->get()->pluck('username', 'id');
96
        $groups = app('rinvex.tenants.tenant')->distinct()->get(['group'])->pluck('group', 'group')->toArray();
97
98
        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...
99
    }
100
101
    /**
102
     * Store new tenant.
103
     *
104
     * @param \Cortex\Tenants\Http\Requests\Adminarea\TenantFormRequest $request
105
     * @param \Cortex\Tenants\Models\Tenant                             $tenant
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $tenant a bit more specific; maybe use Tenant.
Loading history...
106
     *
107
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
108
     */
109
    public function store(TenantFormRequest $request, Tenant $tenant)
110
    {
111
        return $this->process($request, $tenant);
112
    }
113
114
    /**
115
     * Update given tenant.
116
     *
117
     * @param \Cortex\Tenants\Http\Requests\Adminarea\TenantFormRequest $request
118
     * @param \Cortex\Tenants\Models\Tenant                             $tenant
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $tenant a bit more specific; maybe use Tenant.
Loading history...
119
     *
120
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
121
     */
122
    public function update(TenantFormRequest $request, Tenant $tenant)
123
    {
124
        return $this->process($request, $tenant);
125
    }
126
127
    /**
128
     * Process stored/updated tenant.
129
     *
130
     * @param \Illuminate\Foundation\Http\FormRequest $request
131
     * @param \Cortex\Tenants\Models\Tenant           $tenant
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $tenant a bit more specific; maybe use Tenant.
Loading history...
132
     *
133
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
134
     */
135
    protected function process(FormRequest $request, Tenant $tenant)
136
    {
137
        // Prepare required input fields
138
        $data = $request->validated();
139
140
        // Save tenant
141
        $tenant->fill($data)->save();
142
143
        return intend([
144
            'url' => route('adminarea.tenants.index'),
145
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => 'tenant', 'id' => $tenant->slug])],
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...
146
        ]);
147
    }
148
149
    /**
150
     * Destroy given tenant.
151
     *
152
     * @param \Cortex\Tenants\Models\Tenant $tenant
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $tenant a bit more specific; maybe use Tenant.
Loading history...
153
     *
154
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
155
     */
156
    public function destroy(Tenant $tenant)
157
    {
158
        $tenant->delete();
159
160
        return intend([
161
            'url' => route('adminarea.tenants.index'),
162
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => 'tenant', 'id' => $tenant->slug])],
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...
163
        ]);
164
    }
165
}
166