2017-08-08 23:24:55 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories\Eloquent;
|
|
|
|
|
2017-10-06 23:57:53 -05:00
|
|
|
use Pterodactyl\Models\Egg;
|
2017-10-05 23:09:43 -05:00
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2018-01-04 22:49:50 -06:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2017-10-06 23:57:53 -05:00
|
|
|
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
2017-10-03 20:18:27 -05:00
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
2017-08-08 23:24:55 -05:00
|
|
|
|
2017-10-06 23:57:53 -05:00
|
|
|
class EggRepository extends EloquentRepository implements EggRepositoryInterface
|
2017-08-08 23:24:55 -05:00
|
|
|
{
|
|
|
|
/**
|
2018-01-04 22:49:50 -06:00
|
|
|
* Return the model backing this repository.
|
|
|
|
*
|
|
|
|
* @return string
|
2017-08-08 23:24:55 -05:00
|
|
|
*/
|
|
|
|
public function model()
|
|
|
|
{
|
2017-10-06 23:57:53 -05:00
|
|
|
return Egg::class;
|
2017-08-08 23:24:55 -05:00
|
|
|
}
|
2017-08-12 15:29:01 -05:00
|
|
|
|
|
|
|
/**
|
2017-10-06 23:57:53 -05:00
|
|
|
* Return an egg with the variables relation attached.
|
2017-10-03 20:18:27 -05:00
|
|
|
*
|
|
|
|
* @param int $id
|
2017-10-06 23:57:53 -05:00
|
|
|
* @return \Pterodactyl\Models\Egg
|
2017-10-03 20:18:27 -05:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\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
|
|
|
{
|
2018-01-04 22:49:50 -06:00
|
|
|
try {
|
2018-01-06 13:08:20 -06:00
|
|
|
return $this->getBuilder()->with('variables')->findOrFail($id, $this->getColumns());
|
2018-01-04 22:49:50 -06:00
|
|
|
} catch (ModelNotFoundException $exception) {
|
2017-10-03 20:18:27 -05:00
|
|
|
throw new RecordNotFoundException;
|
|
|
|
}
|
2017-08-12 15:29:01 -05:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:09:43 -05:00
|
|
|
/**
|
2017-10-06 23:57:53 -05:00
|
|
|
* Return all eggs and their relations to be used in the daemon API.
|
2017-10-05 23:09:43 -05:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
|
|
*/
|
|
|
|
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.
|
2017-10-03 20:18:27 -05:00
|
|
|
*
|
2017-10-05 23:09:43 -05:00
|
|
|
* @param int|string $value
|
2019-09-05 21:32:57 -07:00
|
|
|
* @param string $column
|
2017-10-06 23:57:53 -05:00
|
|
|
* @return \Pterodactyl\Models\Egg
|
2017-10-03 20:18:27 -05:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\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
|
|
|
{
|
2017-10-05 23:09:43 -05:00
|
|
|
Assert::true((is_digit($value) || is_string($value)), 'First argument passed to getWithCopyAttributes must be an integer or string, received %s.');
|
|
|
|
|
2018-01-04 22:49:50 -06:00
|
|
|
try {
|
|
|
|
return $this->getBuilder()->with('scriptFrom', 'configFrom')->where($column, '=', $value)->firstOrFail($this->getColumns());
|
|
|
|
} catch (ModelNotFoundException $exception) {
|
2017-10-03 20:18:27 -05:00
|
|
|
throw new RecordNotFoundException;
|
|
|
|
}
|
2017-08-12 15:29:01 -05:00
|
|
|
}
|
|
|
|
|
2017-10-03 23:31:04 -05:00
|
|
|
/**
|
|
|
|
* Return all of the data needed to export a service.
|
|
|
|
*
|
|
|
|
* @param int $id
|
2017-10-06 23:57:53 -05:00
|
|
|
* @return \Pterodactyl\Models\Egg
|
2017-10-03 23:31:04 -05:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2017-10-06 23:57:53 -05:00
|
|
|
public function getWithExportAttributes(int $id): Egg
|
2017-10-03 23:31:04 -05:00
|
|
|
{
|
2018-01-04 22:49:50 -06:00
|
|
|
try {
|
2018-01-06 13:08:20 -06:00
|
|
|
return $this->getBuilder()->with('scriptFrom', 'configFrom', 'variables')->findOrFail($id, $this->getColumns());
|
2018-01-04 22:49:50 -06:00
|
|
|
} catch (ModelNotFoundException $exception) {
|
2017-10-03 23:31:04 -05:00
|
|
|
throw new RecordNotFoundException;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-03 20:18:27 -05:00
|
|
|
*
|
|
|
|
* @param int $copyFromId
|
|
|
|
* @param int $service
|
|
|
|
* @return bool
|
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();
|
|
|
|
}
|
2017-08-08 23:24:55 -05:00
|
|
|
}
|