75 lines
3.7 KiB
PHP
75 lines
3.7 KiB
PHP
<?php
|
||
|
||
namespace App\Modules\Machines\Infrastructure\Providers;
|
||
|
||
use App\Models\User;
|
||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||
use App\Modules\Jobs\Domain\Events\TaskCompleted;
|
||
use App\Modules\Machines\Application\Contracts\MachineQueries;
|
||
use App\Modules\Machines\Application\Contracts\MachineTasks;
|
||
use App\Modules\Machines\Application\Contracts\MaintenanceMaterials;
|
||
use App\Modules\Machines\Application\Contracts\MaintenanceQueries;
|
||
use App\Modules\Machines\Application\Contracts\ProductionProcessQueries;
|
||
use App\Modules\Machines\Application\UseCases\MachineWorkList;
|
||
use App\Modules\Machines\Domain\Contracts\MachineRepository;
|
||
use App\Modules\Machines\Domain\Contracts\MaintenanceRepository;
|
||
use App\Modules\Machines\Domain\Contracts\ProductionProcessRepository;
|
||
use App\Modules\Machines\Domain\Contracts\QueueRepository;
|
||
use App\Modules\Machines\Infrastructure\Adapters\JobsForMachines;
|
||
use App\Modules\Machines\Infrastructure\Adapters\StockForMachines;
|
||
use App\Modules\Machines\Infrastructure\Persistence\Repositories\EloquentMachineQueries;
|
||
use App\Modules\Machines\Infrastructure\Persistence\Repositories\EloquentMachineRepository;
|
||
use App\Modules\Machines\Infrastructure\Persistence\Repositories\EloquentMaintenanceQueries;
|
||
use App\Modules\Machines\Infrastructure\Persistence\Repositories\EloquentMaintenanceRepository;
|
||
use App\Modules\Machines\Infrastructure\Persistence\Repositories\EloquentProductionProcesses;
|
||
use App\Modules\Machines\Infrastructure\Persistence\Repositories\EloquentQueueRepository;
|
||
use App\Modules\Users\Domain\Enums\Role;
|
||
use Illuminate\Support\Facades\Event;
|
||
use Illuminate\Support\Facades\Gate;
|
||
use Illuminate\Support\ServiceProvider;
|
||
use Livewire\Livewire;
|
||
|
||
/**
|
||
* Wires the Machines module (machines, their work lists, maintenance and
|
||
* the machine console, spec 19.4–19.6) into Laravel: bindings, routes, the
|
||
* `machines::` Livewire namespace and the `machines.*` abilities.
|
||
*/
|
||
class MachinesServiceProvider extends ServiceProvider
|
||
{
|
||
/**
|
||
* @var array<class-string, class-string>
|
||
*/
|
||
public array $bindings = [
|
||
MachineRepository::class => EloquentMachineRepository::class,
|
||
QueueRepository::class => EloquentQueueRepository::class,
|
||
MachineQueries::class => EloquentMachineQueries::class,
|
||
MachineTasks::class => JobsForMachines::class,
|
||
MaintenanceRepository::class => EloquentMaintenanceRepository::class,
|
||
MaintenanceQueries::class => EloquentMaintenanceQueries::class,
|
||
MaintenanceMaterials::class => StockForMachines::class,
|
||
ProductionProcessQueries::class => EloquentProductionProcesses::class,
|
||
ProductionProcessRepository::class => EloquentProductionProcesses::class,
|
||
];
|
||
|
||
public function boot(): void
|
||
{
|
||
$presentationPath = dirname(__DIR__, 2).'/Presentation';
|
||
|
||
Livewire::addNamespace('machines', viewPath: $presentationPath.'/Livewire');
|
||
|
||
if (! $this->app->routesAreCached()) {
|
||
$this->loadRoutesFrom($presentationPath.'/Routes/web.php');
|
||
}
|
||
|
||
// A task marked done elsewhere leaves its machine's work list.
|
||
Event::listen(TaskCompleted::class, fn (TaskCompleted $event) => app(MachineWorkList::class)->closeTask($event->taskId));
|
||
|
||
$staff = fn (User $user): bool => $user->role !== Role::Subcontractor;
|
||
|
||
// Moving tasks to machines, the console and maintenance records: everyone but subcontractors (spec 19.9).
|
||
Gate::define('machines.use', $staff);
|
||
Gate::define('machines.manage', fn (User $user): bool => in_array($user->role, [Role::Admin, Role::Management], true));
|
||
Gate::define('machines.menu', fn (User $user): bool => $staff($user) && app(CatalogQueries::class)->stockServiceUnitIds() !== []);
|
||
}
|
||
}
|