Skip to content

Commit

Permalink
Add docblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
stancl committed Sep 20, 2019
1 parent 93fc961 commit 509d00f
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

class CacheManager extends BaseCacheManager
{
/**
* Add tags and forward the call to the inner cache store.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
$tags = [config('tenancy.cache.tag_base') . tenant('id')];
Expand Down
3 changes: 3 additions & 0 deletions src/Contracts/TenancyBootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Stancl\Tenancy\Tenant;

/**
* TenancyBootstrappers are classes that make existing code tenant-aware.
*/
interface TenancyBootstrapper
{
public function start(Tenant $tenant);
Expand Down
26 changes: 25 additions & 1 deletion src/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ protected function getDriver(string $connectionName): string
return $this->app['config']["database.connections.$connectionName.driver"];
}

public function switchConnection($connection)
/**
* Switch the application's connection.
*
* @param string $connection
* @return void
*/
public function switchConnection(string $connection)
{
$this->app['config']['database.default'] = $connection;
$this->database->purge();
Expand All @@ -103,6 +109,12 @@ public function ensureTenantCanBeCreated(Tenant $tenant): void
}
}

/**
* Create a database for a tenant.
*
* @param Tenant $tenant
* @return void
*/
public function createDatabase(Tenant $tenant)
{
$database = $tenant->getDatabaseName();
Expand All @@ -115,6 +127,12 @@ public function createDatabase(Tenant $tenant)
}
}

/**
* Delete a tenant's database.
*
* @param Tenant $tenant
* @return void
*/
public function deleteDatabase(Tenant $tenant)
{
$database = $tenant->getDatabaseName();
Expand All @@ -127,6 +145,12 @@ public function deleteDatabase(Tenant $tenant)
}
}

/**
* Get the TenantDatabaseManager for a tenant's database connection.
*
* @param Tenant $tenant
* @return TenantDatabaseManager
*/
protected function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager
{
// todo2 this shouldn't have to create a connection
Expand Down
97 changes: 92 additions & 5 deletions src/Tenant.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ArrayAccess;
use Illuminate\Foundation\Application;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls;
use Stancl\Tenancy\Contracts\StorageDriver;
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
use Stancl\Tenancy\Exceptions\TenantStorageException;
Expand All @@ -18,7 +19,8 @@
*/
class Tenant implements ArrayAccess
{
use Traits\HasArrayAccess;
use Traits\HasArrayAccess,
ForwardsCalls;

/**
* Tenant data. A "cache" of tenant storage.
Expand Down Expand Up @@ -53,6 +55,14 @@ class Tenant implements ArrayAccess
*/
protected $persisted = false;

/**
* Use new() if you don't want to swap dependencies.
*
* @param Application $app
* @param StorageDriver $storage
* @param TenantManager $tenantManager
* @param UniqueIdentifierGenerator $idGenerator
*/
public function __construct(Application $app, StorageDriver $storage, TenantManager $tenantManager, UniqueIdentifierGenerator $idGenerator)
{
$this->app = $app;
Expand All @@ -61,6 +71,12 @@ public function __construct(Application $app, StorageDriver $storage, TenantMana
$this->idGenerator = $idGenerator;
}

/**
* Public constructor.
*
* @param Application $app
* @return self
*/
public static function new(Application $app = null): self
{
$app = $app ?? app();
Expand All @@ -73,11 +89,25 @@ public static function new(Application $app = null): self
);
}

/**
* DO NOT CALL THIS METHOD FROM USERLAND. Used by storage
* drivers to create persisted instances of Tenant.
*
* @param array $data
* @return self
*/
public static function fromStorage(array $data): self
{
return static::new()->withData($data)->persisted(true);
}

/**
* Create a tenant in a single command.
*
* @param string|string[] $domains
* @param array $data
* @return self
*/
public static function create($domains, array $data = []): self
{
return static::new()->withDomains((array) $domains)->withData($data)->save();
Expand All @@ -94,6 +124,11 @@ protected function persisted($persisted = null)
return $this;
}

/**
* Does this model exist in the tenant storage.
*
* @return boolean
*/
public function isPersisted(): bool
{
return $this->persisted;
Expand Down Expand Up @@ -127,13 +162,24 @@ public function removeDomains($domains): self
return $this;
}

/**
* Unassign all domains from the tenant.
*
* @return self
*/
public function clearDomains(): self
{
$this->domains = [];

return $this;
}

/**
* Set (overwrite) the tenant's domains.
*
* @param string|string[] $domains
* @return self
*/
public function withDomains($domains): self
{
$domains = (array) $domains;
Expand All @@ -143,18 +189,34 @@ public function withDomains($domains): self
return $this;
}

/**
* Set (overwrite) tenant data.
*
* @param array $data
* @return self
*/
public function withData(array $data): self
{
$this->data = $data;

return $this;
}

/**
* Generate a random ID.
*
* @return void
*/
public function generateId()
{
$this->id = $this->idGenerator->generate($this->domains, $this->data);
}

/**
* Write the tenant's state to storage.
*
* @return self
*/
public function save(): self
{
if (! isset($this->data['id'])) {
Expand Down Expand Up @@ -188,7 +250,7 @@ public function delete(): self
}

/**
* Unassign all domains from the tenant.
* Unassign all domains from the tenant and write to storage.
*
* @return self
*/
Expand All @@ -201,12 +263,22 @@ public function softDelete(): self
return $this;
}

public function getDatabaseName()
/**
* Get the tenant's database's name.
*
* @return string
*/
public function getDatabaseName(): string
{
return $this->data['_tenancy_db_name'] ?? ($this->app['config']['tenancy.database.prefix'] . $this->id . $this->app['config']['tenancy.database.suffix']);
}

public function getConnectionName()
/**
* Get the tenant's database connection's name.
*
* @return string
*/
public function getConnectionName(): string
{
return $this->data['_tenancy_db_connection'] ?? 'tenant';
}
Expand Down Expand Up @@ -243,6 +315,13 @@ public function get($keys)
return $this->data[$key];
}

/**
* Set a value and write to storage.
*
* @param string|array<string, mixed> $key
* @param mixed $value
* @return self
*/
public function put($key, $value = null): self
{
if ($key === 'id') {
Expand All @@ -268,6 +347,13 @@ public function set($key, $value = null): self
return $this->put($key, $value);
}

/**
* Set a value.
*
* @param string $key
* @param mixed $value
* @return self
*/
public function with(string $key, $value): self
{
$this->data[$key] = $value;
Expand All @@ -285,6 +371,7 @@ public function __set($key, $value)
if ($key === 'id' && isset($this->data['id'])) {
throw new TenantStorageException("Tenant ids can't be changed.");
}

$this->data[$key] = $value;
}

Expand All @@ -294,6 +381,6 @@ public function __call($method, $parameters)
return $this->with(Str::snake(substr($method, 4)), $parameters[0]);
}

// todo throw some exception?
static::throwBadMethodCallException($method);
}
}
Loading

0 comments on commit 509d00f

Please sign in to comment.