mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 06:04:03 +00:00
Ditch models for custom repositories
This commit is contained in:
parent
58f488784f
commit
a7cdfdacb4
7 changed files with 284 additions and 274 deletions
55
src/StorageDrivers/Database/DomainRepository.php
Normal file
55
src/StorageDrivers/Database/DomainRepository.php
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\StorageDrivers\Database;
|
||||
|
||||
use Illuminate\Config\Repository as ConfigRepository;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
|
||||
class DomainRepository extends Repository
|
||||
{
|
||||
public function getTenantIdByDomain(string $domain): string
|
||||
{
|
||||
return $this->where('domain', $domain)->first()->tenant_id ?? null;
|
||||
}
|
||||
|
||||
public function occupied(array $domains): bool
|
||||
{
|
||||
return $this->whereIn('domain', $domains)->exists();
|
||||
}
|
||||
|
||||
public function getTenantDomains($tenant)
|
||||
{
|
||||
$id = $tenant instanceof Tenant ? $tenant->id : $tenant;
|
||||
return $this->where('tenant_id', $id)->get('domain')->toArray();
|
||||
}
|
||||
|
||||
public function insertTenantDomains(Tenant $tenant)
|
||||
{
|
||||
$this->insert(array_map(function ($domain) use ($tenant) {
|
||||
return ['domain' => $domain, 'tenant_id' => $tenant->id];
|
||||
}, $tenant->domains));
|
||||
}
|
||||
|
||||
public function updateTenantDomains(Tenant $tenant)
|
||||
{
|
||||
$originalDomains = $this->domains->getTenantDomains($tenant);
|
||||
$deletedDomains = array_diff($originalDomains, $tenant->domains);
|
||||
$newDomains = array_intersect($originalDomains, $tenant->domains);
|
||||
|
||||
$this->domains->whereIn('domain', $deletedDomains)->delete();
|
||||
|
||||
foreach ($newDomains as $domain) {
|
||||
$this->insert([
|
||||
'tenant_id' => $tenant->id,
|
||||
'domain' => $domain,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function getTable(ConfigRepository $config)
|
||||
{
|
||||
return $config->get('tenancy.storage_drivers.db.table_names.DomainModel') // legacy
|
||||
?? $config->get('tenancy.storage_drivers.db.table_names.domains')
|
||||
?? 'domains';
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue