1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 20:54:04 +00:00

[2.2.0] Timestamps (#213)

* Timestamps

* Apply fixes from StyleCI

* Add void typehint

* wip

* Fix tests

* Apply fixes from StyleCI
This commit is contained in:
Samuel Štancl 2019-11-04 17:56:08 +01:00 committed by GitHub
parent b0c8e92bb4
commit 31c9930c93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 124 additions and 4 deletions

View file

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Features;
use Illuminate\Config\Repository;
use Illuminate\Support\Facades\Date;
use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\Tenant;
use Stancl\Tenancy\TenantManager;
class Timestamps implements Feature
{
/** @var Repository */
protected $config;
public function __construct(Repository $config)
{
$this->config = $config;
}
public function bootstrap(TenantManager $tenantManager): void
{
$tenantManager->hook('tenant.creating', function ($tm, Tenant $tenant) {
$tenant->with('created_at', $this->now());
$tenant->with('updated_at', $this->now());
});
$tenantManager->hook('tenant.updating', function ($tm, Tenant $tenant) {
$tenant->with('updated_at', $this->now());
});
$tenantManager->hook('tenant.softDeleting', function ($tm, Tenant $tenant) {
$tenant->with('deleted_at', $this->now());
});
}
public function now(): string
{
// Add this key to your tenancy.php config if you need to change the format.
return Date::now()->format(
$this->config->get('tenancy.features.timestamps.format') ?? 'c' // ISO 8601
);
}
}

View file

@ -260,10 +260,16 @@ class Tenant implements ArrayAccess
*/
public function softDelete(): self
{
$this->put('_tenancy_original_domains', $this->domains);
$this->manager->event('tenant.softDeleting', $this);
$this->put([
'_tenancy_original_domains' => $this->domains,
]);
$this->clearDomains();
$this->save();
$this->manager->event('tenant.softDeleted', $this);
return $this;
}
@ -328,6 +334,8 @@ class Tenant implements ArrayAccess
*/
public function put($key, $value = null): self
{
$this->manager->event('tenant.updating', $this);
if ($key === 'id') {
throw new TenantStorageException("Tenant ids can't be changed.");
}
@ -348,6 +356,8 @@ class Tenant implements ArrayAccess
$this->data[$key] = $value;
}
$this->manager->event('tenant.updated', $this);
return $this;
}
@ -376,6 +386,8 @@ class Tenant implements ArrayAccess
*/
public function deleteKeys(array $keys): self
{
$this->manager->event('tenant.updating', $this);
if (! $this->storage instanceof CanDeleteKeys) {
throw new NotImplementedException(get_class($this->storage), 'deleteMany',
'This method was added to storage drivers provided by the package in 2.2.0 and will be part of the StorageDriver contract in 3.0.0.'
@ -387,11 +399,13 @@ class Tenant implements ArrayAccess
}
}
$this->manager->event('tenant.updated', $this);
return $this;
}
/**
* Set a value.
* Set a value in the data array without saving into storage.
*
* @param string $key
* @param mixed $value