-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from hans-thomas/feature/adding-role-model-cont…
…ract Create a contract for models that responsable for roles;
- Loading branch information
Showing
10 changed files
with
128 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Hans\Sphinx\Models\Contracts; | ||
|
||
interface RoleMethods | ||
{ | ||
/** | ||
* Find the given id and cache the result. | ||
* | ||
* @param int $id | ||
* | ||
* @return static | ||
*/ | ||
public static function findAndCache(int $id): self; | ||
|
||
/** | ||
* Return version of the current instance. | ||
* | ||
* @return int | ||
*/ | ||
public function getVersion(): int; | ||
|
||
/** | ||
* Increase the version by one unit. | ||
* | ||
* @return bool | ||
*/ | ||
public function increaseVersion(): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Hans\Sphinx\Models\Traits; | ||
|
||
use Illuminate\Support\Facades\Cache; | ||
use Throwable; | ||
|
||
trait RoleMethods | ||
{ | ||
/** | ||
* @param int $id | ||
* | ||
* @return static | ||
*/ | ||
public static function findAndCache(int $id): self | ||
{ | ||
return Cache::rememberForever( | ||
static::cacheKey($id), | ||
fn () => self::query()->findOrFail($id) | ||
); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getVersion(): int | ||
{ | ||
return $this->version ?: self::query()->findOrFail($this->id, ['id', 'version'])->version; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function increaseVersion(): bool | ||
{ | ||
try { | ||
$this->increment('version'); | ||
$this->fill(['version' => $this->getVersion() + 1])->saveQuietly(); | ||
Cache::forget(self::cacheKey($this->id)); | ||
Cache::forever(self::cacheKey($this->id), $this); | ||
} catch (Throwable $e) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Make the unique key for caching the instance. | ||
* | ||
* @param int $id | ||
* | ||
* @return string | ||
*/ | ||
protected static function cacheKey(int $id): string | ||
{ | ||
return "role_cache_$id"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters