connection = $connection; $this->daemonRepository = $daemonRepository; $this->repository = $repository; $this->serverRepository = $serverRepository; } /** * Update the default allocation for a server only if that allocation is currently * assigned to the specified server. * * @param int|\Pterodactyl\Models\Server $server * @param int $allocation * @return \Pterodactyl\Models\Allocation * * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException * @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Pterodactyl\Exceptions\Service\Allocation\AllocationDoesNotBelongToServerException */ public function handle($server, int $allocation): Allocation { if (! $server instanceof Server) { $server = $this->serverRepository->find($server); } $allocations = $this->repository->findWhere([['server_id', '=', $server->id]]); $model = $allocations->filter(function ($model) use ($allocation) { return $model->id === $allocation; })->first(); if (! $model instanceof Allocation) { throw new AllocationDoesNotBelongToServerException; } $this->connection->beginTransaction(); $this->serverRepository->withoutFreshModel()->update($server->id, ['allocation_id' => $model->id]); // Update on the daemon. try { $this->daemonRepository->setServer($server)->update([ 'build' => [ 'default' => [ 'ip' => $model->ip, 'port' => $model->port, ], 'ports|overwrite' => $allocations->groupBy('ip')->map(function ($item) { return $item->pluck('port'); })->toArray(), ], ]); $this->connection->commit(); } catch (RequestException $exception) { $this->connection->rollBack(); throw new DaemonConnectionException($exception); } return $model; } }