Files

87 lines
2.6 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Pterodactyl\Repositories\Eloquent;
2017-10-06 23:57:53 -05:00
use Pterodactyl\Models\Egg;
use Webmozart\Assert\Assert;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
2017-10-06 23:57:53 -05:00
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
2017-10-06 23:57:53 -05:00
class EggRepository extends EloquentRepository implements EggRepositoryInterface
{
/**
* Return the model backing this repository.
*/
2022-10-14 10:59:20 -06:00
public function model(): string
{
2017-10-06 23:57:53 -05:00
return Egg::class;
}
2017-08-12 15:29:01 -05:00
/**
2017-10-06 23:57:53 -05:00
* Return an egg with the variables relation attached.
*
2024-10-21 19:18:20 -06:00
* @throws RecordNotFoundException
2017-08-12 15:29:01 -05:00
*/
2017-10-06 23:57:53 -05:00
public function getWithVariables(int $id): Egg
2017-08-12 15:29:01 -05:00
{
try {
2018-01-06 13:08:20 -06:00
return $this->getBuilder()->with('variables')->findOrFail($id, $this->getColumns());
2022-10-14 10:59:20 -06:00
} catch (ModelNotFoundException) {
2021-01-23 12:33:34 -08:00
throw new RecordNotFoundException();
}
2017-08-12 15:29:01 -05:00
}
/**
2017-10-06 23:57:53 -05:00
* Return all eggs and their relations to be used in the daemon API.
*/
public function getAllWithCopyAttributes(): Collection
{
return $this->getBuilder()->with('scriptFrom', 'configFrom')->get($this->getColumns());
}
2017-08-12 15:29:01 -05:00
/**
2017-10-06 23:57:53 -05:00
* Return an egg with the scriptFrom and configFrom relations loaded onto the model.
*
* @param int|string $value
*
2024-10-21 19:18:20 -06:00
* @throws RecordNotFoundException
2017-08-12 15:29:01 -05:00
*/
2017-10-06 23:57:53 -05:00
public function getWithCopyAttributes($value, string $column = 'id'): Egg
2017-08-12 15:29:01 -05:00
{
2022-10-14 10:59:20 -06:00
Assert::true(is_digit($value) || is_string($value), 'First argument passed to getWithCopyAttributes must be an integer or string, received %s.');
try {
return $this->getBuilder()->with('scriptFrom', 'configFrom')->where($column, '=', $value)->firstOrFail($this->getColumns());
2022-10-14 10:59:20 -06:00
} catch (ModelNotFoundException) {
2021-01-23 12:33:34 -08:00
throw new RecordNotFoundException();
}
2017-08-12 15:29:01 -05:00
}
2017-10-03 23:31:04 -05:00
/**
2022-10-14 10:59:20 -06:00
* Return all the data needed to export a service.
2017-10-03 23:31:04 -05:00
*
2024-10-21 19:18:20 -06:00
* @throws RecordNotFoundException
2017-10-03 23:31:04 -05:00
*/
2017-10-06 23:57:53 -05:00
public function getWithExportAttributes(int $id): Egg
2017-10-03 23:31:04 -05:00
{
try {
2018-01-06 13:08:20 -06:00
return $this->getBuilder()->with('scriptFrom', 'configFrom', 'variables')->findOrFail($id, $this->getColumns());
2022-10-14 10:59:20 -06:00
} catch (ModelNotFoundException) {
2021-01-23 12:33:34 -08:00
throw new RecordNotFoundException();
2017-10-03 23:31:04 -05:00
}
}
2017-08-12 15:29:01 -05:00
/**
2017-10-06 23:57:53 -05:00
* Confirm a copy script belongs to the same nest as the item trying to use it.
2017-08-12 15:29:01 -05:00
*/
2018-05-13 10:50:56 -04:00
public function isCopyableScript(int $copyFromId, int $service): bool
2017-08-12 15:29:01 -05:00
{
return $this->getBuilder()->whereNull('copy_script_from')
->where('id', '=', $copyFromId)
2017-10-06 23:57:53 -05:00
->where('nest_id', '=', $service)
2017-08-12 15:29:01 -05:00
->exists();
}
}