misc_pterodactyl-panel/app/Models/Task.php

128 lines
3.2 KiB
PHP
Raw Normal View History

<?php
2016-12-07 22:46:38 +00:00
namespace Pterodactyl\Models;
2020-02-08 23:23:08 +00:00
use Illuminate\Container\Container;
2017-09-17 04:10:00 +00:00
use Znck\Eloquent\Traits\BelongsToThrough;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2020-02-08 23:23:08 +00:00
use Pterodactyl\Contracts\Extensions\HashidsInterface;
2020-02-08 23:23:08 +00:00
/**
* @property int $id
* @property int $schedule_id
* @property int $sequence_id
* @property string $action
* @property string $payload
* @property int $time_offset
* @property bool $is_queued
* @property bool $continue_on_failure
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property string $hashid
2020-02-08 23:23:08 +00:00
* @property \Pterodactyl\Models\Schedule $schedule
* @property \Pterodactyl\Models\Server $server
2020-02-08 23:23:08 +00:00
*/
class Task extends Model
{
use BelongsToThrough;
/**
* The resource name for this model when it is transformed into an
* API representation using fractal.
*/
2021-01-23 20:33:34 +00:00
public const RESOURCE_NAME = 'schedule_task';
/**
* The default actions that can exist for a task in Pterodactyl.
*/
public const ACTION_POWER = 'power';
public const ACTION_COMMAND = 'command';
public const ACTION_BACKUP = 'backup';
/**
* The table associated with the model.
*/
protected $table = 'tasks';
/**
* Relationships to be updated when this model is updated.
*/
protected $touches = ['schedule'];
/**
* Fields that are mass assignable.
*/
protected $fillable = [
'schedule_id',
2017-09-14 02:46:43 +00:00
'sequence_id',
'action',
'payload',
'time_offset',
'is_queued',
'continue_on_failure',
];
2017-03-19 23:36:50 +00:00
/**
* Cast values to correct type.
*/
protected $casts = [
'id' => 'integer',
'schedule_id' => 'integer',
2017-09-14 02:46:43 +00:00
'sequence_id' => 'integer',
'time_offset' => 'integer',
'is_queued' => 'boolean',
'continue_on_failure' => 'boolean',
2017-03-19 23:36:50 +00:00
];
/**
* Default attributes when creating a new model.
*/
protected $attributes = [
'time_offset' => 0,
'is_queued' => false,
'continue_on_failure' => false,
];
public static array $validationRules = [
'schedule_id' => 'required|numeric|exists:schedules,id',
'sequence_id' => 'required|numeric|min:1',
'action' => 'required|string',
'payload' => 'required_unless:action,backup|string',
'time_offset' => 'required|numeric|between:0,900',
'is_queued' => 'boolean',
'continue_on_failure' => 'boolean',
];
/**
* {@inheritDoc}
*/
public function getRouteKeyName(): string
{
return $this->getKeyName();
}
/**
* Return a hashid encoded string to represent the ID of the task.
*/
public function getHashidAttribute(): string
{
2020-02-08 23:23:08 +00:00
return Container::getInstance()->make(HashidsInterface::class)->encode($this->id);
}
2017-04-15 17:52:43 +00:00
/**
* Return the schedule that a task belongs to.
2017-04-15 17:52:43 +00:00
*/
public function schedule(): BelongsTo
2017-04-15 17:52:43 +00:00
{
return $this->belongsTo(Schedule::class);
2017-04-15 17:52:43 +00:00
}
/**
* Return the server a task is assigned to, acts as a belongsToThrough.
2017-04-15 17:52:43 +00:00
*/
public function server(): \Znck\Eloquent\Relations\BelongsToThrough
{
2017-09-17 04:10:00 +00:00
return $this->belongsToThrough(Server::class, Schedule::class);
}
}