Skip to content

Commit

Permalink
make phpstan happy 😎
Browse files Browse the repository at this point in the history
  • Loading branch information
bezhanSalleh committed Aug 25, 2023
2 parents ee043cc + 4671e55 commit fd2bf79
Showing 1 changed file with 47 additions and 42 deletions.
89 changes: 47 additions & 42 deletions src/Resources/RoleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

namespace BezhanSalleh\FilamentShield\Resources;

use BezhanSalleh\FilamentShield\Contracts\HasShieldPermissions;
use BezhanSalleh\FilamentShield\Facades\FilamentShield;
use BezhanSalleh\FilamentShield\Resources\RoleResource\Pages;
use BezhanSalleh\FilamentShield\Support\Utils;
use Filament\Forms;
use Filament\Tables;
use Livewire\Livewire;
use Filament\Forms\Components\Component;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use Illuminate\Database\Eloquent\Model;
use Filament\Forms\Components\Component;
use BezhanSalleh\FilamentShield\Support\Utils;
use BezhanSalleh\FilamentShield\Facades\FilamentShield;
use BezhanSalleh\FilamentShield\Resources\RoleResource\Pages;
use BezhanSalleh\FilamentShield\Contracts\HasShieldPermissions;
use Illuminate\Support\Str;

class RoleResource extends Resource implements HasShieldPermissions
{
Expand Down Expand Up @@ -93,18 +91,18 @@ public static function form(Form $form): Form
->options(fn (): array => static::experimentalGetPagePermissions())
->searchable()
->live()
->afterStateHydrated(function (Component $component, $livewire, Model $record,Forms\Set $set) {
->afterStateHydrated(function (Component $component, $livewire, Model $record, Forms\Set $set) {
static::experimentalSetPagesStateWhenRecordHasPermission($component, $record);
})
->afterStateUpdated(
fn ($livewire, Forms\Set $set) => static::experimentalToggleSelectAllViaEntities($livewire, $set)
)
->dehydrated(fn($state) => blank($state) ? false : true)
->dehydrated(fn ($state) => blank($state) ? false : true)
->bulkToggleable()
->gridDirection('row')
->columns([
'sm' => 2,
'lg' => 4
'lg' => 4,
])
->columnSpanFull(),
]),
Expand All @@ -117,18 +115,18 @@ public static function form(Form $form): Form
->options(fn (): array => static::experimentalGetWidgetPermissions())
->searchable()
->live()
->afterStateHydrated(function (Component $component, $livewire, Model $record,Forms\Set $set) {
->afterStateHydrated(function (Component $component, $livewire, Model $record, Forms\Set $set) {
static::experimentalSetWidgetsStateWhenRecordHasPermission($component, $record);
})
->afterStateUpdated(
fn ($livewire, Forms\Set $set) => static::experimentalToggleSelectAllViaEntities($livewire, $set)
)
->dehydrated(fn($state) => blank($state) ? false : true)
->dehydrated(fn ($state) => blank($state) ? false : true)
->bulkToggleable()
->gridDirection('row')
->columns([
'sm' => 2,
'lg' => 4
'lg' => 4,
])
->columnSpanFull(),
]),
Expand All @@ -142,19 +140,19 @@ public static function form(Form $form): Form
->options(fn (): array => static::experimentalGetCustomPermissions())
->searchable()
->live()
->afterStateHydrated(function (Component $component, $livewire, Model $record,Forms\Set $set) {
->afterStateHydrated(function (Component $component, $livewire, Model $record, Forms\Set $set) {
static::experimentalSetCustomPermissionsStateWhenRecordHasPermission($component, $record);
static::experimentalToggleSelectAllViaEntities($livewire, $set);
})
->afterStateUpdated(
fn ($livewire, Forms\Set $set) => static::experimentalToggleSelectAllViaEntities($livewire, $set)
)
->dehydrated(fn($state) => blank($state) ? false : true)
->dehydrated(fn ($state) => blank($state) ? false : true)
->bulkToggleable()
->gridDirection('row')
->columns([
'sm' => 2,
'lg' => 4
'lg' => 4,
])
->columnSpanFull(),
]),
Expand Down Expand Up @@ -292,15 +290,15 @@ public static function getResourceEntitiesSchema(): ?array
Forms\Components\CheckboxList::make($entity['resource'])
->label(FilamentShield::getLocalizedResourceLabel($entity['fqcn']))
->hint(Utils::showModelPath($entity['fqcn']))
->options(fn():array => static::experimentalGetEntityPermissions($entity))
->options(fn (): array => static::experimentalGetEntityPermissions($entity))
->live()
->afterStateHydrated(function (Component $component, Model $record) use ($entity) {
static::experimentalSetEntityStateWhenRecordHasPermission($component, $record, $entity);
})
->afterStateUpdated(
fn ($livewire, Forms\Set $set) => static::experimentalToggleSelectAllViaEntities($livewire, $set)
)
->dehydrated(fn($state) => blank($state) ? false : true)
->dehydrated(fn ($state) => blank($state) ? false : true)
->bulkToggleable(),
])
->columnSpan(1);
Expand All @@ -316,16 +314,18 @@ public static function getResourceTabBadge()
->map(fn ($resource) => count(static::experimentalGetEntityPermissions($resource)))
->sum();
}

public static function experimentalGetEntityPermissions(array $entity): array
{
return collect(Utils::getResourcePermissionPrefixes($entity['fqcn']))
->flatMap(fn ($permission) => [
$permission.'_'.$entity['resource'] => FilamentShield::getLocalizedResourcePermissionLabel($permission)
$permission . '_' . $entity['resource'] => FilamentShield::getLocalizedResourcePermissionLabel($permission),
])
->toArray();
->toArray();
}

public static function experimentalSetEntityStateWhenRecordHasPermission(Component $component, Model $record, array $entity) {
public static function experimentalSetEntityStateWhenRecordHasPermission(Component $component, Model $record, array $entity)
{

if (blank($record)) {
return;
Expand All @@ -338,6 +338,7 @@ public static function experimentalSetEntityStateWhenRecordHasPermission(Compone
if ($record->checkPermissionTo($key)) {
$permissions[] = $key;
}

return $permissions;
}, collect())
->toArray()
Expand All @@ -352,7 +353,7 @@ public static function experimentalToggleEntitiesViaSelectAll($livewire, $set, b
if ($state) {
$entitiesComponents
->each(
function (Forms\Components\CheckboxList $component) use($set) {
function (Forms\Components\CheckboxList $component) use ($set) {
$set($component->getName(), array_keys($component->getOptions()));
}
);
Expand All @@ -362,15 +363,17 @@ function (Forms\Components\CheckboxList $component) use($set) {
}
}

public static function experimentalToggleSelectAllViaEntities($livewire, $set) {
public static function experimentalToggleSelectAllViaEntities($livewire, $set)
{
$entitiesStates = collect($livewire->form->getFlatComponents())
->reduce(function ($counts, $component) {
if ($component instanceof Forms\Components\CheckboxList) {
$counts[$component->getName()] = count(array_keys($component->getOptions())) == count($component->getState());
}
return $counts;
}, collect())
->values();
->reduce(function ($counts, $component) {
if ($component instanceof Forms\Components\CheckboxList) {
$counts[$component->getName()] = count(array_keys($component->getOptions())) == count($component->getState());
}

return $counts;
}, collect())
->values();
if ($entitiesStates->containsStrict(false)) {
$set('select_all', false);
} else {
Expand All @@ -382,12 +385,13 @@ public static function experimentalGetWidgetPermissions(): array
{
return collect(FilamentShield::getWidgets())
->flatMap(fn ($widgetPermission) => [
$widgetPermission => FilamentShield::getLocalizedWidgetLabel($widgetPermission)
$widgetPermission => FilamentShield::getLocalizedWidgetLabel($widgetPermission),
])
->toArray();
->toArray();
}

public static function experimentalSetWidgetsStateWhenRecordHasPermission(Component $component, Model $record) {
public static function experimentalSetWidgetsStateWhenRecordHasPermission(Component $component, Model $record)
{

if (blank($record)) {
return;
Expand All @@ -406,12 +410,13 @@ public static function experimentalGetPagePermissions(): array
{
return collect(FilamentShield::getPages())
->flatMap(fn ($pagePermission) => [
$pagePermission => FilamentShield::getLocalizedPageLabel($pagePermission)
$pagePermission => FilamentShield::getLocalizedPageLabel($pagePermission),
])
->toArray();
->toArray();
}

public static function experimentalSetPagesStateWhenRecordHasPermission(Component $component, Model $record) {
public static function experimentalSetPagesStateWhenRecordHasPermission(Component $component, Model $record)
{

if (blank($record)) {
return;
Expand All @@ -430,9 +435,9 @@ public static function experimentalGetCustomPermissions(): array
{
return collect(static::getCustomEntities())
->flatMap(fn ($customPermission) => [
$customPermission => str($customPermission)->headline()->toString()
$customPermission => str($customPermission)->headline()->toString(),
])
->toArray();
->toArray();
}

public static function experimentalSetCustomPermissionsStateWhenRecordHasPermission(Component $component, Model $record)
Expand Down

0 comments on commit fd2bf79

Please sign in to comment.