Passed
Push — master ( e1e86f...c6853e )
by Curtis
05:23 queued 11s
created

MixedConnection   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 14
c 1
b 0
f 0
dl 0
loc 26
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 17 3
A connection() 0 5 1
1
<?php
2
3
namespace App\Service;
4
5
use Illuminate\Support\Facades\DB;
6
use LaravelEnso\Multitenancy\App\Enums\Connections;
7
8
class MixedConnection
9
{
10
    public static function set($user, $tenant)
11
    {
12
        if (! $user->belongsToAdminGroup() || $tenant) {
13
            self::connection(Connections::Tenant);
14
        } else {
15
            self::connection('mysql');
16
        }
17
        // This is test for tenant db
18
        self::connection(Connections::Tenant);
19
20
        $key = 'database.default';
21
        $value = Connections::Mixed;
22
        config([$key => $value]);
23
24
        DB::purge(Connections::Mixed);
25
26
        DB::reconnect(Connections::Mixed);
27
    }
28
29
    private static function connection($connection)
30
    {
31
        $key = 'database.connections.'.Connections::Mixed.'.database';
32
        $value = config("database.connections.{$connection}.database");
33
        config([$key => $value]);
34
    }
35
}
36