, total_allocated: int, unallocated: int} */ public function execute(Scenario $scenario, int $amount): array { $draws = $this->pipelineAllocationService->allocateInflow($scenario, $amount); /** @var array $bucketLookup */ $bucketLookup = $scenario->buckets->keyBy('id')->all(); DB::transaction(function () use ($draws, $bucketLookup) { foreach ($draws as $draw) { $bucket = $bucketLookup[$draw->bucket_id]; $bucket->increment('starting_amount', (int) $draw->amount); } }); $allocations = $draws->map(function ($draw) use ($bucketLookup) { $bucket = $bucketLookup[$draw->bucket_id]; return [ 'bucket_id' => $bucket->uuid, 'bucket_name' => $bucket->name, 'bucket_type' => $bucket->type->value, 'allocated_amount' => (int) $draw->amount, ]; })->values(); /** @var int $totalAllocated */ $totalAllocated = $draws->sum('amount'); return [ 'allocations' => $allocations, 'total_allocated' => $totalAllocated, 'unallocated' => $amount - $totalAllocated, ]; } }