rawilk /
laravel-modules
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Rawilk\LaravelModules\Models; |
||
| 4 | |||
| 5 | use Illuminate\Database\Eloquent\Collection; |
||
| 6 | use Illuminate\Database\Eloquent\Model; |
||
| 7 | use Rawilk\LaravelModules\Contracts\ModuleModel; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Rawilk\LaravelModules\Models\Module |
||
| 11 | * |
||
| 12 | * @property int $id |
||
| 13 | * @property string $name |
||
| 14 | * @property string $path |
||
| 15 | * @propperty string $description |
||
| 16 | * @property array $keywords |
||
| 17 | * @property boolean $is_active |
||
| 18 | * @property int $order |
||
| 19 | * @property array $providers |
||
| 20 | * @property array $aliases |
||
| 21 | * @property array $files |
||
| 22 | * @property array $requires |
||
| 23 | * @mixin \Eloquent |
||
| 24 | */ |
||
| 25 | class Module extends Model implements ModuleModel |
||
| 26 | { |
||
| 27 | protected $fillable = [ |
||
| 28 | 'name', 'path', 'alias', 'description', 'keywords', 'is_active', |
||
| 29 | 'order', 'providers', 'aliases', 'files', 'requires' |
||
| 30 | ]; |
||
| 31 | |||
| 32 | protected $casts = [ |
||
| 33 | 'keywords' => 'array', |
||
| 34 | 'is_active' => 'boolean', |
||
| 35 | 'order' => 'integer', |
||
| 36 | 'providers' => 'array', |
||
| 37 | 'aliases' => 'array', |
||
| 38 | 'files' => 'array', |
||
| 39 | 'requires' => 'array' |
||
| 40 | ]; |
||
| 41 | |||
| 42 | public static function allDisabled(array $columns = ['*']): Collection |
||
| 43 | { |
||
| 44 | return (new static)->newQuery()->where('is_active', false)->get($columns); |
||
| 45 | } |
||
| 46 | |||
| 47 | public static function allEnabled(array $columns = ['*']): Collection |
||
| 48 | { |
||
| 49 | return (new static)->newQuery()->where('is_active', true)->get($columns); |
||
| 50 | } |
||
| 51 | |||
| 52 | public static function allModules(array $columns = ['*']): Collection |
||
| 53 | { |
||
| 54 | return (new static)->newQuery()->get($columns); |
||
| 55 | } |
||
| 56 | |||
| 57 | public static function disable(string $name): void |
||
| 58 | { |
||
| 59 | (new static) |
||
| 60 | ->newQuery() |
||
| 61 | ->where('name', $name) |
||
| 62 | ->update(['is_active' => false]); |
||
| 63 | } |
||
| 64 | |||
| 65 | public static function enable(string $name): void |
||
| 66 | { |
||
| 67 | (new static) |
||
| 68 | ->newQuery() |
||
| 69 | ->where('name', $name) |
||
| 70 | ->update(['is_active' => true]); |
||
| 71 | } |
||
| 72 | |||
| 73 | public static function findByAlias(string $alias, array $columns = ['*']): ?ModuleModel |
||
| 74 | { |
||
| 75 | return (new static)->newQuery() |
||
| 76 | ->where('alias', $alias) |
||
| 77 | ->first($columns); |
||
| 78 | } |
||
| 79 | |||
| 80 | public static function findModule(string $name, array $columns = ['*']): ?ModuleModel |
||
| 81 | { |
||
| 82 | return (new static)->newQuery() |
||
| 83 | ->where('name', $name) |
||
| 84 | ->first($columns); |
||
| 85 | } |
||
| 86 | |||
| 87 | public static function getByStatus(bool $status, array $columns = ['*']): Collection |
||
| 88 | { |
||
| 89 | return (new static)->newQuery() |
||
| 90 | ->where('is_active', $status) |
||
| 91 | ->get($columns); |
||
| 92 | } |
||
| 93 | |||
| 94 | public static function getCount(): int |
||
| 95 | { |
||
| 96 | return (new static)->newQuery()->count(); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 97 | } |
||
| 98 | |||
| 99 | public static function getOrdered(string $direction = 'asc', array $columns = ['*']): Collection |
||
| 100 | { |
||
| 101 | return (new static)->newQuery() |
||
| 102 | ->where('is_active', true) |
||
| 103 | ->orderBy('order', $direction) |
||
| 104 | ->get($columns); |
||
| 105 | } |
||
| 106 | |||
| 107 | public static function moduleExists(string $name): bool |
||
| 108 | { |
||
| 109 | return (new static) |
||
| 110 | ->newQuery() |
||
| 111 | ->where('name', $name) |
||
| 112 | ->count() > 0; |
||
| 113 | } |
||
| 114 | |||
| 115 | public function getName(): string |
||
| 116 | { |
||
| 117 | return $this->name; |
||
| 118 | } |
||
| 119 | |||
| 120 | public function hasStatus(bool $status): bool |
||
| 121 | { |
||
| 122 | return $this->is_active === $status; |
||
| 123 | } |
||
| 124 | |||
| 125 | public function isDisabled(): bool |
||
| 126 | { |
||
| 127 | return $this->hasStatus(false); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function isEnabled(): bool |
||
| 131 | { |
||
| 132 | return $this->hasStatus(true); |
||
| 133 | } |
||
| 134 | } |
||
| 135 |