111 lines
3 KiB
PHP
111 lines
3 KiB
PHP
<?php
|
|
|
|
namespace Pterodactyl\Transformers\Api\Application;
|
|
|
|
use Pterodactyl\Models\Node;
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
|
|
|
class NodeTransformer extends BaseTransformer
|
|
{
|
|
/**
|
|
* List of resources that can be included.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $availableIncludes = ['allocations', 'location', 'servers'];
|
|
|
|
/**
|
|
* Return the resource name for the JSONAPI output.
|
|
*/
|
|
public function getResourceName(): string
|
|
{
|
|
return Node::RESOURCE_NAME;
|
|
}
|
|
|
|
/**
|
|
* Return a node transformed into a format that can be consumed by the
|
|
* external administrative API.
|
|
*/
|
|
public function transform(Node $model): array
|
|
{
|
|
$response = $model->toArray();
|
|
|
|
$response[$model->getUpdatedAtColumn()] = $this->formatTimestamp($model->updated_at);
|
|
$response[$model->getCreatedAtColumn()] = $this->formatTimestamp($model->created_at);
|
|
|
|
$resources = $model->servers()->select(['memory', 'disk'])->get();
|
|
|
|
$response['allocated_resources'] = [
|
|
'memory' => $resources->sum('memory'),
|
|
'disk' => $resources->sum('disk'),
|
|
];
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* Return the nodes associated with this location.
|
|
*
|
|
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
|
|
*
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
|
*/
|
|
public function includeAllocations(Node $node)
|
|
{
|
|
if (!$this->authorize(AdminAcl::RESOURCE_ALLOCATIONS)) {
|
|
return $this->null();
|
|
}
|
|
|
|
$node->loadMissing('allocations');
|
|
|
|
return $this->collection(
|
|
$node->getRelation('allocations'),
|
|
$this->makeTransformer(AllocationTransformer::class),
|
|
'allocation'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Return the nodes associated with this location.
|
|
*
|
|
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
|
|
*
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
|
*/
|
|
public function includeLocation(Node $node)
|
|
{
|
|
if (!$this->authorize(AdminAcl::RESOURCE_LOCATIONS)) {
|
|
return $this->null();
|
|
}
|
|
|
|
$node->loadMissing('location');
|
|
|
|
return $this->item(
|
|
$node->getRelation('location'),
|
|
$this->makeTransformer(LocationTransformer::class),
|
|
'location'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Return the nodes associated with this location.
|
|
*
|
|
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
|
|
*
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
|
*/
|
|
public function includeServers(Node $node)
|
|
{
|
|
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
|
|
return $this->null();
|
|
}
|
|
|
|
$node->loadMissing('servers');
|
|
|
|
return $this->collection(
|
|
$node->getRelation('servers'),
|
|
$this->makeTransformer(ServerTransformer::class),
|
|
'server'
|
|
);
|
|
}
|
|
}
|