Skip to content

Commit 0bd20f1

Browse files
authored
Merge pull request #9 from hans-thomas/feature/adding-role-model-contract
Create a contract for models that responsable for roles;
2 parents 870841c + 2af317d commit 0bd20f1

10 files changed

Lines changed: 128 additions & 65 deletions

File tree

.styleci.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@ use-tabs: false
66
finder:
77
exclude:
88
- "docs"
9-
- "modules"
10-
- "node_modules"
11-
- "nova"
12-
- "nova-components"
13-
- "storage"
14-
- "spark"
159
- "vendor"
1610
name: "*.php"
1711
not-name:
18-
- "*.blade.php"
1912
- "_ide_helper.php"

docs/content/docs/facade.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ draft = false
44
weight = 10
55
description = "To make it easy to use Sphinx."
66
title = "Facade"
7-
bref= "There is a facade class to make working easier. this facade contains several methods that we will introduce in continue."
7+
bref= "There is a facade class to make working easier. this facade contains several methods that we will introduce in continue"
88
toc = false
99
+++
1010

docs/content/docs/installation.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ draft = false
44
weight = 9
55
description = "Installation guidance to install and setup Sphinx."
66
title = "Installation"
7-
bref= "To install Sphinx, follow the below steps."
7+
bref= "To install Sphinx, follow the below steps"
88
toc = false
99
+++
1010

@@ -85,4 +85,23 @@ And finally, you can set the `jwt` guard as default.
8585
],
8686
```
8787

88-
All sets. enjoy!
88+
All sets.
89+
90+
## Role model
91+
92+
Sphinx expects the role model of your project, contains requested method
93+
on `Hans\Sphinx\Models\Contracts\RoleMethods` interface class. So, you should implement this contract and for your
94+
convenient, there is the `Hans\Sphinx\Models\Traits\RoleMethods` trait class that contains all the methods that you must
95+
implement.
96+
97+
```
98+
use Hans\Sphinx\Models\Contracts\RoleMethods as RoleContract;
99+
use Hans\Sphinx\Models\Traits\RoleMethods;
100+
use Spatie\Permission\Models\Role;
101+
102+
class RoleDelegate extends Role implements RoleContract {
103+
use RoleMethods;
104+
105+
// ...
106+
}
107+
```

docs/content/docs/trait.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ draft = false
44
weight = 20
55
description = "A bunch of useful and handy methods."
66
title = "Trait"
7-
bref= "Sphinx adds some useful and necessary methods through the trait. there is the list of available methods."
7+
bref= "Sphinx adds some useful and necessary methods through the trait. there is the list of available methods"
88
toc = false
99
+++
1010

docs/layouts/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ <h1>{{.Title}}</h1>
88
<p>{{.Description}}</p>
99
</div>
1010
<div id="action-buttons">
11-
<a class="button primary big" href="/docs">Documentation</a>
11+
<a class="button primary big" href="/docs/installation">Installation</a>
1212
<a class="button outline big" href="https://github.com/hans-thomas/sphinx"
1313
onclick="_gaq.push(['_trackEvent', 'sphinx', 'github']);">View on Github</a>
1414
</div>

src/Drivers/Constraints/RoleIdValidator.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Hans\Sphinx\Exceptions\SphinxErrorCode;
66
use Hans\Sphinx\Exceptions\SphinxException;
7+
use Hans\Sphinx\Models\Contracts\RoleMethods as RoleContract;
78
use Lcobucci\JWT\Token;
89
use Lcobucci\JWT\Validation\Constraint;
910
use Symfony\Component\HttpFoundation\Response as ResponseAlias;
@@ -35,7 +36,16 @@ public function assert(Token $token): void
3536
);
3637
}
3738

38-
$role = app(sphinx_config('role_model'))->findAndCache($role_id);
39+
$model = app(sphinx_config('role_model'));
40+
if (!$model instanceof RoleContract) {
41+
$modelClass = get_class($model);
42+
43+
throw new SphinxException(
44+
"Role model [$modelClass] must implement the ".RoleContract::class.' contract.',
45+
SphinxErrorCode::MUST_IMPLEMENT_ROLE_CONTRACT,
46+
);
47+
}
48+
$role = $model->findAndCache($role_id);
3949

4050
if ($role->getVersion() != $role_version) {
4151
throw new SphinxException(

src/Exceptions/SphinxErrorCode.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ class SphinxErrorCode
1717
public const FAILED_TO_SET_HEADER = 1013;
1818
public const FAILED_TO_CREATE_TOKEN = 1014;
1919
public const FAILED_TO_CREATE_REFRESH_TOKEN = 1015;
20+
public const MUST_IMPLEMENT_ROLE_CONTRACT = 1016;
2021
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Hans\Sphinx\Models\Contracts;
4+
5+
interface RoleMethods
6+
{
7+
/**
8+
* Find the given id and cache the result.
9+
*
10+
* @param int $id
11+
*
12+
* @return static
13+
*/
14+
public static function findAndCache(int $id): self;
15+
16+
/**
17+
* Return version of the current instance.
18+
*
19+
* @return int
20+
*/
21+
public function getVersion(): int;
22+
23+
/**
24+
* Increase the version by one unit.
25+
*
26+
* @return bool
27+
*/
28+
public function increaseVersion(): bool;
29+
}

src/Models/Traits/RoleMethods.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Hans\Sphinx\Models\Traits;
4+
5+
use Illuminate\Support\Facades\Cache;
6+
use Throwable;
7+
8+
trait RoleMethods
9+
{
10+
/**
11+
* @param int $id
12+
*
13+
* @return static
14+
*/
15+
public static function findAndCache(int $id): self
16+
{
17+
return Cache::rememberForever(
18+
static::cacheKey($id),
19+
fn () => self::query()->findOrFail($id)
20+
);
21+
}
22+
23+
/**
24+
* @return int
25+
*/
26+
public function getVersion(): int
27+
{
28+
return $this->version ?: self::query()->findOrFail($this->id, ['id', 'version'])->version;
29+
}
30+
31+
/**
32+
* @return bool
33+
*/
34+
public function increaseVersion(): bool
35+
{
36+
try {
37+
$this->increment('version');
38+
$this->fill(['version' => $this->getVersion() + 1])->saveQuietly();
39+
Cache::forget(self::cacheKey($this->id));
40+
Cache::forever(self::cacheKey($this->id), $this);
41+
} catch (Throwable $e) {
42+
return false;
43+
}
44+
45+
return true;
46+
}
47+
48+
/**
49+
* Make the unique key for caching the instance.
50+
*
51+
* @param int $id
52+
*
53+
* @return string
54+
*/
55+
protected static function cacheKey(int $id): string
56+
{
57+
return "role_cache_$id";
58+
}
59+
}

tests/skeleton/laravel-10.x/app/Models/RoleDelegate.php

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,11 @@
22

33
namespace App\Models;
44

5-
use Hans\Horus\Helpers\Enums\CacheEnum;
6-
use Illuminate\Support\Facades\Cache;
5+
use Hans\Sphinx\Models\Contracts\RoleMethods as RoleContract;
6+
use Hans\Sphinx\Models\Traits\RoleMethods;
77
use Spatie\Permission\Models\Role;
8-
use Throwable;
98

10-
// TODO: not documented in horus
11-
class RoleDelegate extends Role
9+
class RoleDelegate extends Role implements RoleContract
1210
{
13-
/**
14-
* @param int $id
15-
*
16-
* @return static
17-
*/
18-
public static function findAndCache(int $id): self
19-
{
20-
return Cache::rememberForever(
21-
self::cacheKey($id),
22-
fn () => self::query()->findOrFail($id)
23-
);
24-
}
25-
26-
/**
27-
* @return int
28-
*/
29-
public function getVersion(): int
30-
{
31-
return $this->version ?: self::query()->findOrFail($this->id, ['id', 'version'])->version;
32-
}
33-
34-
/**
35-
* @return bool
36-
*/
37-
public function increaseVersion(): bool
38-
{
39-
try {
40-
$this->increment('version');
41-
$this->fill(['version' => $this->getVersion() + 1])->saveQuietly();
42-
Cache::forget(self::cacheKey($this->id));
43-
Cache::forever(self::cacheKey($this->id), $this);
44-
} catch (Throwable $e) {
45-
return false;
46-
}
47-
48-
return true;
49-
}
50-
51-
/**
52-
* @param int $id
53-
*
54-
* @return string
55-
*/
56-
private static function cacheKey(int $id): string
57-
{
58-
return CacheEnum::ROLE->value."_$id";
59-
}
11+
use RoleMethods;
6012
}

0 commit comments

Comments
 (0)