Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
14 changes: 7 additions & 7 deletions src/classes/App/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@

class ExceptionHandler
{
private $getDetails;
private $changeStatus;
private $stringTools;
private GetDetails $getDetails;
private ChangeStatus $changeStatus;
private StringTools $stringTools;

/**
* @Inject
*/
public function inject(GetDetails $getDetails, ChangeStatus $changeStatus, StringTools $stringTools)
public function inject(GetDetails $getDetails, ChangeStatus $changeStatus, StringTools $stringTools) :void
{
$this->getDetails = $getDetails;
$this->changeStatus = $changeStatus;
$this->stringTools = $stringTools;
}

public function register()
public function register() :void
{
set_exception_handler([$this, "handle"]);
}

public function handle($exception)
public function handle($exception) :void
{
$message = $exception->getMessage();

Expand All @@ -39,7 +39,7 @@ public function handle($exception)
$hostId = $this->getDetails->getIdByUrlMatch($url);
if (is_numeric($hostId)) {
http_response_code(205);
$this->changeStatus->setOffline($hostId);
$this->changeStatus->setOffline((int) $hostId);
}
}

Expand Down
40 changes: 23 additions & 17 deletions src/classes/App/RouteApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@

class RouteApi
{
private $container;
private $recordAction;
private $getDetails;
private $hostList;
private $fetchAllowedProjects;
private $fetchUserDetails;
private $fetchUserProject;
private $invalidateToken;
private Container $container;
private RecordAction $recordAction;
private GetDetails $getDetails;
private HostList $hostList;
private FetchAllowedProjects $fetchAllowedProjects;
private FetchUserDetails $fetchUserDetails;
private FetchUserProject $fetchUserProject;
private InvalidateToken $invalidateToken;

private $project;
private $userId;
private ?string $project;
private int $userId;


public function __construct(
Expand Down Expand Up @@ -50,12 +50,12 @@ public function getRequestedProject()
return $this->project;
}

public function getUserId()
public function getUserId() :int
{
return $this->userId;
}

public function route($pathParts, $headers, $returnResult = false)
public function route(array $pathParts, array $headers, bool $returnResult = false)
{
$userId = $headers["userid"];

Expand All @@ -75,7 +75,7 @@ public function route($pathParts, $headers, $returnResult = false)

unset($pathParts[$methodkey]);

$controllerStr = "dhope0000\\LXDClient\\Controllers\\" . implode($pathParts, "\\");
$controllerStr = "dhope0000\\LXDClient\\Controllers\\" . implode("\\", $pathParts);
if (!class_exists($controllerStr)) {
throw new \Exception("End point not found", 1);
} elseif (method_exists($controllerStr, $method) !== true) {
Expand All @@ -90,8 +90,14 @@ public function route($pathParts, $headers, $returnResult = false)
$this->recordAction->record($userId, $controllerStr . "\\" . $method, $params);
}

$callback = array($controller, $method);

if (!is_callable($callback)) {
throw new \Exception("Cant find route", 1);
}

// TODO Pass provided arguments to controller
$data = call_user_func_array(array($controller, $method), $params);
$data = call_user_func_array($callback, $params);

if ($returnResult) {
return $data;
Expand All @@ -101,7 +107,7 @@ public function route($pathParts, $headers, $returnResult = false)
echo json_encode($data);
}

public function orderParams($passedArguments, $class, $method, int $userId, $headers)
public function orderParams(array $passedArguments, string $class, string $method, int $userId, array $headers) :array
{
$reflectedMethod = new \ReflectionMethod($class, $method);
$paramaters = $reflectedMethod->getParameters();
Expand All @@ -124,7 +130,7 @@ public function orderParams($passedArguments, $class, $method, int $userId, $hea
$hasDefault = $param->isDefaultValueAvailable();

$type = $param->getType();
if (!empty($type)) {
if (!empty($type) && $type instanceof \ReflectionNamedType) {
$type = $type->getName();
}

Expand Down Expand Up @@ -186,7 +192,7 @@ public function orderParams($passedArguments, $class, $method, int $userId, $hea
return $o;
}

private function canAccessProject($allowedProjects, $hostId, $project)
private function canAccessProject(array $allowedProjects, int $hostId, string $project) :void
{
if (!is_string($project) || strlen($project) == 0) {
throw new \Exception("Cant work out which project to use", 1);
Expand Down
17 changes: 13 additions & 4 deletions src/classes/App/RouteAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class RouteAssets
{
private $extensionMapping = [
private array $extensionMapping = [
"css"=>"text/css",
"js"=>"text/javascript",
"png"=>"image/png",
Expand All @@ -12,20 +12,29 @@ class RouteAssets
"woff2"=>"font/woff2"
];

public function route($path)
public function route(array $path) :void
{
$this->outputFile($path);
}

public function outputFile($path)
public function outputFile(array $path) :void
{
$path = __DIR__ . "/../../" . implode($path, "/");
$path = __DIR__ . "/../../" . implode("/", $path);
if (strpos($path, "?") !== false) {
$path = substr($path, 0, strpos($path, "?"));
}

if (!is_file($path)) {
throw new \Exception("Cant find asset", 1);
}

//get the last-modified-date of this very file
$lastModified=filemtime($path);

if (!$lastModified) {
throw new \Exception("Cant find asset", 1);
}

//get a unique hash of this file (etag)
$etagFile = md5_file($path);
//get the HTTP_IF_MODIFIED_SINCE header if set
Expand Down
23 changes: 14 additions & 9 deletions src/classes/App/RouteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@

class RouteController
{
private $validateToken;
private $userSession;
private $logUserIn;
private $routeApi;
private $routeView;
private $routeAssets;
private $session;
private $fetchUserDetails;
private ValidateToken $validateToken;
private UserSession $userSession;
private LogUserIn $logUserIn;
private RouteApi $routeApi;
private RouteView $routeView;
private RouteAssets $routeAssets;
private Session $session;
private FetchUserDetails $fetchUserDetails;

public $loginError = null;
public ?string $loginError = null;

public function __construct(
UserSession $userSession,
Expand Down Expand Up @@ -118,6 +118,11 @@ public function routeRequest($explodedPath)
$path = "";
if (isset($explodedPath[0])) {
$parts = parse_url($explodedPath[0]);

if ($parts == false) {
throw new \Exception("Couldn't parse '{$explodedPath[0]}'", 1);
}

$path = $parts["path"];
}

Expand Down
7 changes: 4 additions & 3 deletions src/classes/App/RouteView.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

class RouteView
{
private $container;

/** @phpstan-ignore-next-line */
private Container $container;

public function __construct(Container $container)
{
$this->container = $container;
}

public function route($pathParts)
public function route(array $pathParts)
{
if (empty($pathParts)) {
require __DIR__ . "/../../views/index.php";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

class DownloadHistoryController
{
private $downloadHistory;
private DownloadHistory $downloadHistory;

public function __construct(DownloadHistory $downloadHistory)
{
$this->downloadHistory = $downloadHistory;
}

public function download(int $userId)
public function download(int $userId) :array
{
return $this->downloadHistory->download($userId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

class GetBackupsOverviewController
{
private $getBackupsOverview;
private GetBackupsOverview $getBackupsOverview;

public function __construct(GetBackupsOverview $getBackupsOverview)
{
$this->getBackupsOverview = $getBackupsOverview;
}

public function get($userId)
public function get(int $userId) :array
{
return $this->getBackupsOverview->get($userId);
}
Expand Down
4 changes: 2 additions & 2 deletions src/classes/Controllers/Backups/RestoreBackupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class RestoreBackupController implements \dhope0000\LXDClient\Interfaces\RecordAction
{
private $restoreBackup;
private RestoreBackup $restoreBackup;

public function __construct(RestoreBackup $restoreBackup)
{
Expand All @@ -17,7 +17,7 @@ public function __construct(RestoreBackup $restoreBackup)
/**
* @Route("", name="Restore Local Backup To Host")
*/
public function restore(int $userId, int $backupId, Host $targetHost)
public function restore(int $userId, int $backupId, Host $targetHost) :array
{
$response = $this->restoreBackup->restore($userId, $backupId, $targetHost);
return ["state"=>"success", "message"=>"Restored Backup", "lxdResponse"=>$response];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

class GetStrategiesController
{
private $fetchStrategies;
private FetchStrategies $fetchStrategies;

public function __construct(FetchStrategies $fetchStrategies)
{
$this->fetchStrategies = $fetchStrategies;
}

public function get()
public function get() :array
{
return $this->fetchStrategies->fetchAll();
}
Expand Down
4 changes: 2 additions & 2 deletions src/classes/Controllers/CloudConfig/CreateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class CreateController implements \dhope0000\LXDClient\Interfaces\RecordAction
{
private $create;
private Create $create;

public function __construct(Create $create)
{
Expand All @@ -15,7 +15,7 @@ public function __construct(Create $create)
/**
* @Route("", name="Create Cloud Config")
*/
public function create(string $name, string $namespace, $description = "")
public function create(string $name, string $namespace, string $description = "")
{
$this->create->create($name, $namespace, $description);
return ["state"=>"success", "message"=>"Created cloud config"];
Expand Down
6 changes: 3 additions & 3 deletions src/classes/Controllers/CloudConfig/DeleteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

class DeleteController implements \dhope0000\LXDClient\Interfaces\RecordAction
{
private $deleteCloudConfig;
private DeleteCloudConfig $deleteCloudConfig;

public function __construct(DeleteCloudConfig $deleteCloudConfig)
{
$this->deleteCloudConfig = $deleteCloudConfig;
}
/**
* @Route("", name="Delete Cloud Config")
*/
public function delete(int $cloudConfigId)
public function delete(int $cloudConfigId) :array
{
$this->deleteCloudConfig->delete($cloudConfigId);
return ["state"=>"success", "message"=>"Deleted Cloud Config"];
Expand Down
10 changes: 5 additions & 5 deletions src/classes/Controllers/CloudConfig/DeployController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

class DeployController implements \dhope0000\LXDClient\Interfaces\RecordAction
{
private $deployConfigToContainer;
private DeployConfigToContainer $deployConfigToContainer;

public function __construct(DeployConfigToContainer $deploy)
{
$this->deployConfigToContainer = $deploy;
Expand All @@ -20,10 +20,10 @@ public function deploy(
HostsCollection $hosts,
string $containerName,
int $cloudConfigId,
$additionalProfiles = [],
$gpus = [],
array $additionalProfiles = [],
array $gpus = [],
string $project = ""
) {
) :array {
$response = $this->deployConfigToContainer->deploy(
$hosts,
$containerName,
Expand Down
6 changes: 3 additions & 3 deletions src/classes/Controllers/CloudConfig/GetAllController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

class GetAllController
{
private $getConfigs;
private GetConfigs $getConfigs;

public function __construct(GetConfigs $getConfigs)
{
$this->getConfigs = $getConfigs;
}

public function getAll()
public function getAll() :array
{
return $this->getConfigs->getAll();
}
Expand Down
Loading