Completed
Push — master ( 872a00...e2f753 )
by Troy
01:33
created

DashboardController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 53
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A index() 0 8 3
A selectTenant() 0 12 1
A changeTenant() 0 6 1
1
<?php
2
3
namespace MultiTenantLaravel\App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
7
class DashboardController extends Controller
8
{
9
    /**
10
     * Create a new controller instance.
11
     *
12
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
13
     */
14
    public function __construct()
15
    {
16
        $this->middleware('multi-tenant');
17
    }
18
19
    /**
20
     * Show the user dashboard with option to select which tenant to manage
21
     * or redirect them to their currently active tenant
22
     *
23
     * @return \Illuminate\Http\Response
24
     */
25
    public function index()
26
    {
27
        if (\Auth::user()->owns()->count() === 1 || session()->has('tenant.id')) {
28
            return view('multi-tenant::tenant.home');
29
        }
30
31
        return view('multi-tenant::dashboard');
32
    }
33
34
    /**
35
     * Add the selected tenant to the users session
36
     */
37
    public function selectTenant()
38
    {
39
        $id = request()->get('id');
40
41
        $tenant = config('multi-tenant.tenant_class')::findOrFail($id);
42
43
        request()->session()->put('tenant', [
44
            'id' => $tenant->id
45
        ]);
46
47
        return redirect()->back();
48
    }
49
50
    /**
51
     * Remove the selected tenant from the users session
52
     */
53
    public function changeTenant()
54
    {
55
        request()->session()->forget('tenant');
56
57
        return redirect('/');
58
    }
59
}
60