misc_pterodactyl-panel/app/Transformers/Api/Application/NodeTransformer.php

110 lines
3.1 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Node;
2018-01-13 20:08:19 +00:00
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Transformers\Api\Transformer;
class NodeTransformer extends Transformer
{
/**
* List of resources that can be included.
*
* @var array
*/
2021-02-16 01:48:10 +00:00
protected $availableIncludes = ['allocations', 'database_host', 'location', 'mounts', 'servers'];
public function getResourceName(): string
{
return Node::RESOURCE_NAME;
}
public function transform(Node $model): array
{
$response = $model->toArray();
2018-01-13 20:08:19 +00:00
2021-08-07 20:25:35 +00:00
$response[$model->getUpdatedAtColumn()] = self::formatTimestamp($model->updated_at);
$response[$model->getCreatedAtColumn()] = self::formatTimestamp($model->created_at);
2018-01-13 20:08:19 +00:00
$resources = $model->servers()->select(['memory', 'disk'])->get();
$response['allocated_resources'] = [
'memory' => $resources->sum('memory'),
'disk' => $resources->sum('disk'),
];
2018-01-13 20:08:19 +00:00
return $response;
}
/**
2021-02-16 01:48:10 +00:00
* Return the allocations associated with this node.
*
2018-01-13 20:08:19 +00:00
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*/
public function includeAllocations(Node $node)
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_ALLOCATIONS)) {
2018-01-13 20:08:19 +00:00
return $this->null();
}
return $this->collection($node->allocations, new AllocationTransformer());
}
/**
2021-02-16 01:48:10 +00:00
* Return the database host associated with this node.
*
2018-01-13 20:08:19 +00:00
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
2021-02-16 01:48:10 +00:00
*/
public function includeDatabaseHost(Node $node)
{
if (!$this->authorize(AdminAcl::RESOURCE_DATABASE_HOSTS) || is_null($node->databaseHost)) {
2021-02-16 01:48:10 +00:00
return $this->null();
}
return $this->item($node->databaseHost, new DatabaseHostTransformer());
2021-02-16 01:48:10 +00:00
}
/**
* Return the location associated with this node.
*
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
public function includeLocation(Node $node)
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_LOCATIONS)) {
2018-01-13 20:08:19 +00:00
return $this->null();
}
return $this->item($node->location, new LocationTransformer());
}
/**
2021-02-16 01:48:10 +00:00
* Return the mounts associated with this node.
*
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*/
public function includeMounts(Node $node)
{
if (!$this->authorize(AdminAcl::RESOURCE_MOUNTS)) {
return $this->null();
}
return $this->collection($node->mounts, new MountTransformer());
2021-02-16 01:48:10 +00:00
}
/**
* Return the servers associated with this node.
*
2018-01-13 20:08:19 +00:00
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*/
public function includeServers(Node $node)
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
2018-01-13 20:08:19 +00:00
return $this->null();
}
return $this->collection($node->servers, new ServerTransformer());
}
}