|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LinkRestrictedAccess; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use LinkRestrictedAccess\Models\RestrictedLink; |
|
7
|
|
|
use LinkRestrictedAccess\Models\RestrictedLinkOpenAction; |
|
8
|
|
|
|
|
9
|
|
|
class RestrictedAccess |
|
10
|
|
|
{ |
|
11
|
|
|
const MODEL_KEY_LINK = 'link'; |
|
12
|
|
|
const MODEL_KEY_OPEN = 'open'; |
|
13
|
|
|
|
|
14
|
|
|
public static bool $runsMigrations = true; |
|
15
|
|
|
|
|
16
|
|
|
public static bool $registersRoutes = true; |
|
17
|
|
|
|
|
18
|
|
|
protected static array $models = [ |
|
19
|
|
|
self::MODEL_KEY_LINK => RestrictedLink::class, |
|
20
|
|
|
self::MODEL_KEY_OPEN => RestrictedLinkOpenAction::class, |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Disable provided by packages migrations. |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public static function ignoreMigrations(): static |
|
27
|
|
|
{ |
|
28
|
1 |
|
static::$runsMigrations = false; |
|
29
|
|
|
|
|
30
|
1 |
|
return new static; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Disable provided by packages routes. |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public static function ignoreRoutes(): static |
|
37
|
|
|
{ |
|
38
|
1 |
|
static::$registersRoutes = false; |
|
39
|
|
|
|
|
40
|
1 |
|
return new static; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $key |
|
45
|
|
|
* @param string $modelClass |
|
46
|
|
|
* @return class-string<static> |
|
|
|
|
|
|
47
|
|
|
* @throws \Exception |
|
48
|
|
|
*/ |
|
49
|
3 |
|
public static function useModel(string $key, string $modelClass): string |
|
50
|
|
|
{ |
|
51
|
3 |
|
if (!in_array($key, array_keys(static::$models))) { |
|
52
|
1 |
|
throw new \Exception( |
|
53
|
1 |
|
"Incorrect model key [{$key}], allowed keys are: " . implode(', ', array_keys(static::$models)) |
|
54
|
1 |
|
); |
|
55
|
|
|
} |
|
56
|
2 |
|
if (!is_subclass_of($modelClass, Model::class)) { |
|
57
|
1 |
|
throw new \Exception("Class should be a model [{$modelClass}]"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
static::$models[$key] = $modelClass; |
|
61
|
|
|
|
|
62
|
1 |
|
return static::class; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $key |
|
67
|
|
|
* @return class-string<Model|RestrictedLink|RestrictedLinkOpenAction> |
|
|
|
|
|
|
68
|
|
|
* @throws \Exception |
|
69
|
|
|
*/ |
|
70
|
15 |
|
public static function modelClass(string $key): string |
|
71
|
|
|
{ |
|
72
|
15 |
|
return static::$models[$key] ?? throw new \Exception( |
|
73
|
15 |
|
"Incorrect model key [{$key}], allowed keys are: " . implode(', ', array_keys(static::$models)) |
|
74
|
15 |
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $key |
|
79
|
|
|
* @param array $attributes |
|
80
|
|
|
* @return Model|RestrictedLink|RestrictedLinkOpenAction |
|
81
|
|
|
* @throws \Exception |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public static function model(string $key, array $attributes = []): Model |
|
84
|
|
|
{ |
|
85
|
1 |
|
$modelClass = static::modelClass($key); |
|
86
|
|
|
|
|
87
|
|
|
/** @var Model $model */ |
|
88
|
1 |
|
$model = new $modelClass($attributes); |
|
89
|
|
|
|
|
90
|
1 |
|
return $model; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return class-string<RestrictedLink> |
|
|
|
|
|
|
96
|
|
|
* @throws \Exception |
|
97
|
|
|
*/ |
|
98
|
15 |
|
public static function restrictedLinkModel(): string |
|
99
|
|
|
{ |
|
100
|
15 |
|
return static::modelClass(self::MODEL_KEY_LINK); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return class-string<RestrictedLinkOpenAction> |
|
|
|
|
|
|
106
|
|
|
* @throws \Exception |
|
107
|
|
|
*/ |
|
108
|
7 |
|
public static function linkOpenActionModel(): string |
|
109
|
|
|
{ |
|
110
|
7 |
|
return static::modelClass(self::MODEL_KEY_OPEN); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|