Skip to content

upgraded to native enums #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Enums/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace LaravelEnso\Multitenancy\Enums;

enum Connection: string
{
case System = 'system';
case Tenant = 'tenant';
case Mixed = 'mixed';
case Testing = 'sqlite';
}
13 changes: 0 additions & 13 deletions src/Enums/Connections.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Jobs/DropTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use LaravelEnso\Companies\Models\Company;
use LaravelEnso\Multitenancy\Enums\Connections;
use LaravelEnso\Multitenancy\Enums\Connection;
use LaravelEnso\Multitenancy\Services\Tenant;

class DropTables implements ShouldQueue
Expand All @@ -29,7 +29,7 @@ public function handle()
{
Tenant::set($this->tenant);

DB::connection(Connections::Tenant)
DB::connection(Connection::Tenant->value)
->getSchemaBuilder()
->dropAllTables();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Jobs/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Artisan;
use LaravelEnso\Companies\Models\Company;
use LaravelEnso\Multitenancy\Enums\Connections;
use LaravelEnso\Multitenancy\Enums\Connection;
use LaravelEnso\Multitenancy\Services\Tenant;

class Migrate implements ShouldQueue
Expand All @@ -30,7 +30,7 @@ public function handle()
Tenant::set($this->tenant);

Artisan::call('migrate', [
'--database' => Connections::Tenant,
'--database' => Connection::Tenant->value,
'--path' => '/database/migrations/tenant',
'--force' => true,
]);
Expand Down
14 changes: 7 additions & 7 deletions src/Services/MixedConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
namespace LaravelEnso\Multitenancy\Services;

use Illuminate\Support\Facades\DB;
use LaravelEnso\Multitenancy\Enums\Connections;
use LaravelEnso\Multitenancy\Enums\Connection;

class MixedConnection
{
public static function set($user, $tenant)
{
if (! $user->belongsToAdminGroup() || $tenant) {
self::connection(Connections::Tenant);
if (!$user->belongsToAdminGroup() || $tenant) {
self::connection(Connection::Tenant->value);
} else {
self::connection(Connections::System);
self::connection(Connection::System->value);
}

DB::purge(Connections::Mixed);
DB::purge(Connection::Mixed->value);

DB::reconnect(Connections::Mixed);
DB::reconnect(Connection::Mixed->value);
}

private static function connection($connection)
{
$key = 'database.connections.'.Connections::Mixed;
$key = 'database.connections.' . Connection::Mixed->value;
$value = config("database.connections.{$connection}");
config([$key => $value]);
}
Expand Down
16 changes: 8 additions & 8 deletions src/Services/Tenant.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use LaravelEnso\Companies\Models\Company;
use LaravelEnso\Multitenancy\Enums\Connections;
use LaravelEnso\Multitenancy\Enums\Connection;

class Tenant
{
private static $tenantPrefix;

public static function set(Company $company)
{
$key = 'database.connections.'.Connections::Tenant.'.database';
$value = self::tenantPrefix().$company->id;
$key = 'database.connections.' . Connection::Tenant->value . '.database';
$value = self::tenantPrefix() . $company->id;
config([$key => $value]);

DB::purge(Connections::Tenant);
DB::purge(Connection::Tenant->value);

DB::reconnect(Connections::Tenant);
DB::reconnect(Connection::Tenant->value);
}

public static function get()
Expand All @@ -29,17 +29,17 @@ public static function get()

public static function tenantDatabase()
{
return config('database.connections.'.Connections::Tenant.'.database');
return config('database.connections.' . Connection::Tenant->value . '.database');
}

private static function tenantId()
{
return (int) Str::replaceFirst(Connections::Tenant, '', self::tenantDatabase());
return (int) Str::replaceFirst(Connection::Tenant->value, '', self::tenantDatabase());
}

private static function tenantPrefix()
{
if (! isset(self::$tenantPrefix)) {
if (!isset(self::$tenantPrefix)) {
self::$tenantPrefix = self::tenantDatabase();
}

Expand Down
14 changes: 7 additions & 7 deletions src/Traits/MixedConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace LaravelEnso\Multitenancy\Traits;

use LaravelEnso\Multitenancy\Enums\Connections;
use LaravelEnso\Multitenancy\Enums\Connection;

trait MixedConnection
{
Expand All @@ -11,23 +11,23 @@ public function __construct()
parent::__construct(...func_get_args());

if (app()->environment('testing')) {
$this->connection = Connections::Testing;
$this->connection = Connection::Testing->value;

return;
}

$this->connection = empty(config('database.connections.'.Connections::Mixed))
? Connections::System
: Connections::Mixed;
$this->connection = empty(config('database.connections.' . Connection::Mixed->value))
? Connection::System->value
: Connection::Mixed->value;
}

public function hasTenantConnection()
{
return $this->getConnection()->getName() === Connections::Tenant;
return $this->getConnection()->getName() === Connection::Tenant->value;
}

public function hasSystemConnection()
{
return $this->getConnection()->getName() === Connections::System;
return $this->getConnection()->getName() === Connection::System->value;
}
}
6 changes: 3 additions & 3 deletions src/Traits/SystemConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace LaravelEnso\Multitenancy\Traits;

use LaravelEnso\Multitenancy\Enums\Connections;
use LaravelEnso\Multitenancy\Enums\Connection;

trait SystemConnection
{
Expand All @@ -11,7 +11,7 @@ public function __construct()
parent::__construct(...func_get_args());

$this->connection = app()->environment('testing')
? Connections::Testing
: Connections::System;
? Connection::Testing->value
: Connection::System->value;
}
}
6 changes: 3 additions & 3 deletions src/Traits/SystemResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
namespace LaravelEnso\Multitenancy\Traits;

use Illuminate\Support\Facades\DB;
use LaravelEnso\Multitenancy\Enums\Connections;
use LaravelEnso\Multitenancy\Enums\Connection;

trait SystemResolver
{
public function systemTable(string $table)
{
return $this->systemDatabase().'.'.$table;
return $this->systemDatabase() . '.' . $table;
}

public function systemDatabase()
{
return DB::connection(Connections::System)
return DB::connection(Connection::System->value)
->getDatabaseName();
}
}
6 changes: 3 additions & 3 deletions src/Traits/TenantConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace LaravelEnso\Multitenancy\Traits;

use LaravelEnso\Multitenancy\Enums\Connections;
use LaravelEnso\Multitenancy\Enums\Connection;

trait TenantConnection
{
Expand All @@ -11,7 +11,7 @@ public function __construct()
parent::__construct(...func_get_args());

$this->connection = app()->environment('testing')
? Connections::Testing
: Connections::Tenant;
? Connection::Testing->value
: Connection::Tenant->value;
}
}
6 changes: 3 additions & 3 deletions src/Traits/TenantResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
namespace LaravelEnso\Multitenancy\Traits;

use Illuminate\Support\Facades\DB;
use LaravelEnso\Multitenancy\Enums\Connections;
use LaravelEnso\Multitenancy\Enums\Connection;

trait TenantResolver
{
public function tenantTable(string $table)
{
return $this->tenantDatabase().'.'.$table;
return $this->tenantDatabase() . '.' . $table;
}

public function tenantDatabase()
{
return DB::connection(Connections::Tenant)
return DB::connection(Connection::Tenant->value)
->getDatabaseName();
}
}