Add phpstan level 7(#544)

This commit is contained in:
Saeed Vaziry 2025-03-12 13:31:10 +01:00 committed by GitHub
parent c22bb1fa80
commit 493cbb0849
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
437 changed files with 4505 additions and 2193 deletions

42
.github/workflows/code-quality.yml vendored Normal file
View File

@ -0,0 +1,42 @@
name: code-quality
on:
push:
branches:
- 2.x
pull_request:
branches:
- 2.x
jobs:
tests:
runs-on: ubuntu-22.04
strategy:
fail-fast: true
matrix:
php: [ 8.2 ]
steps:
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest
- name: Run PHPStan
run: ./vendor/bin/phpstan analyse

View File

@ -10,6 +10,9 @@
class CreateCronJob
{
/**
* @param array<string, mixed> $input
*/
public function create(Server $server, array $input): CronJob
{
$cronJob = new CronJob([
@ -28,6 +31,10 @@ public function create(Server $server, array $input): CronJob
return $cronJob;
}
/**
* @param array<string, mixed> $input
* @return array<string, array<mixed>>
*/
public static function rules(array $input, Server $server): array
{
$rules = [

View File

@ -5,11 +5,15 @@
use App\Enums\DatabaseStatus;
use App\Models\Database;
use App\Models\Server;
use App\Models\Service;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
class CreateDatabase
{
/**
* @param array<string, mixed> $input
*/
public function create(Server $server, array $input): Database
{
$database = new Database([
@ -18,8 +22,12 @@ public function create(Server $server, array $input): Database
'collation' => $input['collation'],
'name' => $input['name'],
]);
/** @var Service $service */
$service = $server->database();
/** @var \App\SSH\Services\Database\Database $databaseHandler */
$databaseHandler = $server->database()->handler();
$databaseHandler = $service->handler();
$databaseHandler->create($database->name, $database->charset, $database->collation);
$database->status = DatabaseStatus::READY;
$database->save();
@ -34,6 +42,9 @@ public function create(Server $server, array $input): Database
}
/**
* @param array<string, mixed> $input
* @return array<string, mixed>
*
* @throws ValidationException
*/
public static function rules(Server $server, array $input): array

View File

@ -5,6 +5,7 @@
use App\Enums\DatabaseUserStatus;
use App\Models\DatabaseUser;
use App\Models\Server;
use App\Models\Service;
use App\SSH\Services\Database\Database;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
@ -12,6 +13,9 @@
class CreateDatabaseUser
{
/**
* @param array<string, mixed> $input
* @param array<string> $links
*
* @throws ValidationException
*/
public function create(Server $server, array $input, array $links = []): DatabaseUser
@ -23,8 +27,12 @@ public function create(Server $server, array $input, array $links = []): Databas
'host' => (isset($input['remote']) && $input['remote']) || isset($input['host']) ? $input['host'] : 'localhost',
'databases' => $links,
]);
/** @var Service $service */
$service = $server->database();
/** @var Database $databaseHandler */
$databaseHandler = $server->database()->handler();
$databaseHandler = $service->handler();
$databaseHandler->createUser(
$databaseUser->username,
$databaseUser->password,
@ -41,6 +49,9 @@ public function create(Server $server, array $input, array $links = []): Databas
}
/**
* @param array<string, mixed> $input
* @return array<string, mixed>
*
* @throws ValidationException
*/
public static function rules(Server $server, array $input): array

View File

@ -4,12 +4,17 @@
use App\Models\Database;
use App\Models\Server;
use App\Models\Service;
class DeleteDatabase
{
public function delete(Server $server, Database $database): void
{
$server->database()->handler()->delete($database->name);
/** @var Service $service */
$service = $server->database();
/** @var \App\SSH\Services\Database\Database $handler */
$handler = $service->handler();
$handler->delete($database->name);
$database->delete();
}
}

View File

@ -4,12 +4,18 @@
use App\Models\DatabaseUser;
use App\Models\Server;
use App\Models\Service;
use App\SSH\Services\Database\Database;
class DeleteDatabaseUser
{
public function delete(Server $server, DatabaseUser $databaseUser): void
{
$server->database()->handler()->deleteUser($databaseUser->username, $databaseUser->host);
/** @var Service $service */
$service = $server->database();
/** @var Database $handler */
$handler = $service->handler();
$handler->deleteUser($databaseUser->username, $databaseUser->host);
$databaseUser->delete();
}
}

View File

@ -5,12 +5,16 @@
use App\Models\Database;
use App\Models\DatabaseUser;
use App\Models\Server;
use App\Models\Service;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
class LinkUser
{
/**
* @param array<string, mixed> $input
* @return DatabaseUser $databaseUser
*
* @throws ValidationException
*/
public function link(DatabaseUser $databaseUser, array $input): DatabaseUser
@ -29,14 +33,20 @@ public function link(DatabaseUser $databaseUser, array $input): DatabaseUser
$databaseUser->databases = $input['databases'];
/** @var Service $service */
$service = $databaseUser->server->database();
/** @var \App\SSH\Services\Database\Database $handler */
$handler = $service->handler();
// Unlink the user from all databases
$databaseUser->server->database()->handler()->unlink(
$handler->unlink(
$databaseUser->username,
$databaseUser->host
);
// Link the user to the selected databases
$databaseUser->server->database()->handler()->link(
$handler->link(
$databaseUser->username,
$databaseUser->host,
$databaseUser->databases
@ -49,6 +59,10 @@ public function link(DatabaseUser $databaseUser, array $input): DatabaseUser
return $databaseUser;
}
/**
* @param array<string, mixed> $input
* @return array<string, mixed>
*/
public static function rules(Server $server, array $input): array
{
return [

View File

@ -14,6 +14,8 @@
class ManageBackup
{
/**
* @param array<string, mixed> $input
*
* @throws AuthorizationException
* @throws ValidationException
*/
@ -35,6 +37,9 @@ public function create(Server $server, array $input): Backup
return $backup;
}
/**
* @param array<string, mixed> $input
*/
public function update(Backup $backup, array $input): void
{
$backup->interval = $input['interval'] == 'custom' ? $input['custom_interval'] : $input['interval'];
@ -47,7 +52,7 @@ public function delete(Backup $backup): void
$backup->status = BackupStatus::DELETING;
$backup->save();
dispatch(function () use ($backup) {
dispatch(function () use ($backup): void {
$files = $backup->files;
foreach ($files as $file) {
$file->status = BackupFileStatus::DELETING;
@ -60,6 +65,10 @@ public function delete(Backup $backup): void
});
}
/**
* @param array<string, mixed> $input
* @return array<string, mixed>
*/
public static function rules(Server $server, array $input): array
{
$rules = [

View File

@ -28,7 +28,7 @@ public function delete(BackupFile $file): void
$file->status = BackupFileStatus::DELETING;
$file->save();
dispatch(function () use ($file) {
dispatch(function () use ($file): void {
$file->deleteFile();
});
}

View File

@ -5,9 +5,13 @@
use App\Enums\BackupFileStatus;
use App\Models\BackupFile;
use App\Models\Database;
use App\Models\Service;
class RestoreBackup
{
/**
* @param array<string, mixed> $input
*/
public function restore(BackupFile $backupFile, array $input): void
{
/** @var Database $database */
@ -16,19 +20,24 @@ public function restore(BackupFile $backupFile, array $input): void
$backupFile->restored_to = $database->name;
$backupFile->save();
dispatch(function () use ($backupFile, $database) {
dispatch(function () use ($backupFile, $database): void {
/** @var Service $service */
$service = $database->server->database();
/** @var \App\SSH\Services\Database\Database $databaseHandler */
$databaseHandler = $database->server->database()->handler();
$databaseHandler = $service->handler();
$databaseHandler->restoreBackup($backupFile, $database->name);
$backupFile->status = BackupFileStatus::RESTORED;
$backupFile->restored_at = now();
$backupFile->save();
})->catch(function () use ($backupFile) {
})->catch(function () use ($backupFile): void {
$backupFile->status = BackupFileStatus::RESTORE_FAILED;
$backupFile->save();
})->onConnection('ssh');
}
/**
* @return array<string, array<string>>
*/
public static function rules(): array
{
return [

View File

@ -6,6 +6,7 @@
use App\Enums\BackupStatus;
use App\Models\Backup;
use App\Models\BackupFile;
use App\Models\Service;
use App\SSH\Services\Database\Database;
use Illuminate\Support\Str;
@ -20,9 +21,11 @@ public function run(Backup $backup): BackupFile
]);
$file->save();
dispatch(function () use ($file, $backup) {
dispatch(function () use ($file, $backup): void {
/** @var Service $service */
$service = $backup->server->database();
/** @var Database $databaseHandler */
$databaseHandler = $file->backup->server->database()->handler();
$databaseHandler = $service->handler();
$databaseHandler->runBackup($file);
$file->status = BackupFileStatus::CREATED;
$file->save();
@ -31,7 +34,7 @@ public function run(Backup $backup): BackupFile
$backup->status = BackupStatus::RUNNING;
$backup->save();
}
})->catch(function () use ($file, $backup) {
})->catch(function () use ($file, $backup): void {
$backup->status = BackupStatus::FAILED;
$backup->save();
$file->status = BackupFileStatus::FAILED;

View File

@ -11,6 +11,8 @@
class FetchFiles
{
/**
* @param array<string, mixed> $input
*
* @throws SSHError
*/
public function fetch(User $user, Server $server, array $input): void
@ -24,6 +26,9 @@ public function fetch(User $user, Server $server, array $input): void
);
}
/**
* @return array<string, array<string>>
*/
public static function rules(Server $server): array
{
return [

View File

@ -5,10 +5,16 @@
use App\Enums\FirewallRuleStatus;
use App\Models\FirewallRule;
use App\Models\Server;
use App\Models\Service;
use App\SSH\Services\Firewall\Firewall;
use Exception;
class ManageRule
{
/**
* @param array<string, mixed> $input
* @return FirewallRule $rule
*/
public function create(Server $server, array $input): FirewallRule
{
$sourceAny = $input['source_any'] ?? empty($input['source'] ?? null);
@ -30,6 +36,10 @@ public function create(Server $server, array $input): FirewallRule
return $rule;
}
/**
* @param array<string, mixed> $input
* @return FirewallRule $rule
*/
public function update(FirewallRule $rule, array $input): FirewallRule
{
$sourceAny = $input['source_any'] ?? empty($input['source'] ?? null);
@ -56,18 +66,20 @@ public function delete(FirewallRule $rule): void
dispatch(fn () => $this->applyRule($rule));
}
protected function applyRule($rule): void
protected function applyRule(FirewallRule $rule): void
{
try {
/** @var Service $service */
$service = $rule->server->firewall();
/** @var Firewall $handler */
$handler = $rule->server->firewall()->handler();
$handler = $service->handler();
$handler->applyRules();
} catch (\Exception $e) {
} catch (Exception) {
$rule->server->firewallRules()
->where('status', '!=', FirewallRuleStatus::READY)
->update(['status' => FirewallRuleStatus::FAILED]);
throw $e;
return;
}
if ($rule->status === FirewallRuleStatus::DELETING) {
@ -80,6 +92,9 @@ protected function applyRule($rule): void
$rule->save();
}
/**
* @return array<string, array<string>>
*/
public static function rules(): array
{
return [

View File

@ -11,6 +11,10 @@
class GetMetrics
{
/**
* @param array<string, mixed> $input
* @return Collection<int, mixed>
*/
public function filter(Server $server, array $input): Collection
{
if (isset($input['from']) && isset($input['to']) && $input['from'] === $input['to']) {
@ -32,6 +36,9 @@ public function filter(Server $server, array $input): Collection
);
}
/**
* @return Collection<int, mixed>
*/
private function metrics(
Server $server,
Carbon $fromDate,
@ -57,13 +64,16 @@ private function metrics(
->groupByRaw('date_interval')
->orderBy('date_interval')
->get()
->map(function ($item) {
->map(function ($item): \stdClass {
$item->date = Carbon::parse($item->date)->format('Y-m-d H:i');
return $item;
});
}
/**
* @param array<string, mixed> $input
*/
private function getFromDate(array $input): Carbon
{
if ($input['period'] === 'custom') {
@ -73,6 +83,9 @@ private function getFromDate(array $input): Carbon
return Carbon::parse('-'.convert_time_format($input['period']));
}
/**
* @param array<string, mixed> $input
*/
private function getToDate(array $input): Carbon
{
if ($input['period'] === 'custom') {
@ -82,6 +95,9 @@ private function getToDate(array $input): Carbon
return Carbon::now();
}
/**
* @param array<string, mixed> $input
*/
private function getInterval(array $input): Expression
{
if ($input['period'] === 'custom') {
@ -107,6 +123,10 @@ private function getInterval(array $input): Expression
return DB::raw("strftime('%Y-%m-%d 00:00:00', created_at) as date_interval");
}
/**
* @param array<string, mixed> $input
* @return array<string, array<string>>
*/
public static function rules(array $input): array
{
$rules = [

View File

@ -3,19 +3,29 @@
namespace App\Actions\Monitoring;
use App\Models\Server;
use App\Models\Service;
use App\SSH\Services\ServiceInterface;
class UpdateMetricSettings
{
/**
* @param array<string, mixed> $input
*/
public function update(Server $server, array $input): void
{
/** @var Service $service */
$service = $server->monitoring();
$data = $service->handler()->data();
/** @var ServiceInterface $handler */
$handler = $service->handler();
$data = $handler->data();
$data['data_retention'] = $input['data_retention'];
$service->type_data = $data;
$service->save();
}
/**
* @return array<string, array<string>>
*/
public static function rules(): array
{
return [

View File

@ -3,24 +3,38 @@
namespace App\Actions\NodeJS;
use App\Enums\ServiceStatus;
use App\Exceptions\SSHError;
use App\Models\Server;
use App\Models\Service;
use App\SSH\Services\NodeJS\NodeJS;
use Illuminate\Validation\ValidationException;
class ChangeDefaultCli
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
* @throws SSHError
*/
public function change(Server $server, array $input): void
{
$this->validate($server, $input);
/** @var Service $service */
$service = $server->nodejs($input['version']);
/** @var NodeJS $handler */
$handler = $service->handler();
$handler->setDefaultCli();
$server->defaultService('nodejs')->update(['is_default' => 0]);
$server->defaultService('nodejs')?->update(['is_default' => 0]);
$service->update(['is_default' => 1]);
$service->update(['status' => ServiceStatus::READY]);
}
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function validate(Server $server, array $input): void
{
if (! isset($input['version']) || ! in_array($input['version'], $server->installedNodejsVersions())) {

View File

@ -10,6 +10,9 @@
class InstallNewNodeJsVersion
{
/**
* @param array<string, mixed> $input
*/
public function install(Server $server, array $input): void
{
$nodejs = new Service([
@ -23,15 +26,18 @@ public function install(Server $server, array $input): void
]);
$nodejs->save();
dispatch(function () use ($nodejs) {
dispatch(function () use ($nodejs): void {
$nodejs->handler()->install();
$nodejs->status = ServiceStatus::READY;
$nodejs->save();
})->catch(function () use ($nodejs) {
})->catch(function () use ($nodejs): void {
$nodejs->delete();
})->onConnection('ssh');
}
/**
* @return array<string, array<string>>
*/
public static function rules(Server $server): array
{
return [

View File

@ -10,6 +10,11 @@
class UninstallNodeJS
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function uninstall(Server $server, array $input): void
{
$this->validate($server, $input);
@ -19,16 +24,18 @@ public function uninstall(Server $server, array $input): void
$nodejs->status = ServiceStatus::UNINSTALLING;
$nodejs->save();
dispatch(function () use ($nodejs) {
dispatch(function () use ($nodejs): void {
$nodejs->handler()->uninstall();
$nodejs->delete();
})->catch(function () use ($nodejs) {
})->catch(function () use ($nodejs): void {
$nodejs->status = ServiceStatus::FAILED;
$nodejs->save();
})->onConnection('ssh');
}
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
private function validate(Server $server, array $input): void

View File

@ -11,8 +11,9 @@
class AddChannel
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
* @throws Exception
*/
public function add(User $user, array $input): void
{
@ -42,13 +43,19 @@ public function add(User $user, array $input): void
} catch (Exception $e) {
$channel->delete();
throw $e;
throw ValidationException::withMessages([
'provider' => $e->getMessage(),
]);
}
$channel->connected = true;
$channel->save();
}
/**
* @param array<string, mixed> $input
* @return array<string, mixed>
*/
public static function rules(array $input): array
{
$rules = [
@ -59,9 +66,13 @@ public static function rules(array $input): array
'label' => 'required',
];
return array_merge($rules, static::providerRules($input));
return array_merge($rules, self::providerRules($input));
}
/**
* @param array<string, mixed> $input
* @return array<string, array<string>>
*/
private static function providerRules(array $input): array
{
if (! isset($input['provider'])) {

View File

@ -7,6 +7,9 @@
class EditChannel
{
/**
* @param array<string, mixed> $input
*/
public function edit(NotificationChannel $notificationChannel, User $user, array $input): void
{
$notificationChannel->fill([
@ -16,6 +19,10 @@ public function edit(NotificationChannel $notificationChannel, User $user, array
$notificationChannel->save();
}
/**
* @param array<string, mixed> $input
* @return array<string, string>
*/
public static function rules(array $input): array
{
return [

View File

@ -3,24 +3,37 @@
namespace App\Actions\PHP;
use App\Enums\ServiceStatus;
use App\Exceptions\SSHError;
use App\Models\Server;
use App\Models\Service;
use App\SSH\Services\PHP\PHP;
use Illuminate\Validation\ValidationException;
class ChangeDefaultCli
{
/**
* @param array<string, mixed> $input
*
* @throws SSHError
*/
public function change(Server $server, array $input): void
{
$this->validate($server, $input);
/** @var Service $service */
$service = $server->php($input['version']);
/** @var PHP $handler */
$handler = $service->handler();
$handler->setDefaultCli();
$server->defaultService('php')->update(['is_default' => 0]);
$server->defaultService('php')?->update(['is_default' => 0]);
$service->update(['is_default' => 1]);
$service->update(['status' => ServiceStatus::READY]);
}
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function validate(Server $server, array $input): void
{
if (! isset($input['version']) || ! in_array($input['version'], $server->installedPHPVersions())) {

View File

@ -4,17 +4,25 @@
use App\Enums\PHPIniType;
use App\Models\Server;
use App\Models\Service;
use App\SSH\Services\PHP\PHP;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Throwable;
class GetPHPIni
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function getIni(Server $server, array $input): string
{
$this->validate($server, $input);
/** @var Service $php */
$php = $server->php($input['version']);
try {
@ -22,13 +30,18 @@ public function getIni(Server $server, array $input): string
$handler = $php->handler();
return $handler->getPHPIni($input['type']);
} catch (\Throwable $e) {
} catch (Throwable $e) {
throw ValidationException::withMessages(
['ini' => $e->getMessage()]
);
}
}
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function validate(Server $server, array $input): void
{
Validator::make($input, [

View File

@ -10,6 +10,9 @@
class InstallNewPHP
{
/**
* @param array<string, mixed> $input
*/
public function install(Server $server, array $input): void
{
$php = new Service([
@ -26,15 +29,18 @@ public function install(Server $server, array $input): void
]);
$php->save();
dispatch(function () use ($php) {
dispatch(function () use ($php): void {
$php->handler()->install();
$php->status = ServiceStatus::READY;
$php->save();
})->catch(function () use ($php) {
})->catch(function () use ($php): void {
$php->delete();
})->onConnection('ssh');
}
/**
* @return array<string, array<string>>
*/
public static function rules(Server $server): array
{
return [

View File

@ -2,7 +2,6 @@
namespace App\Actions\PHP;
use App\Exceptions\SSHCommandError;
use App\Models\Server;
use App\Models\Service;
use App\SSH\Services\PHP\PHP;
@ -11,6 +10,11 @@
class InstallPHPExtension
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function install(Server $server, array $input): Service
{
/** @var Service $service */
@ -23,20 +27,17 @@ public function install(Server $server, array $input): Service
}
$typeData = $service->type_data;
$typeData['extensions'] = $typeData['extensions'] ?? [];
$typeData['extensions'] ??= [];
$typeData['extensions'][] = $input['extension'];
$service->type_data = $typeData;
$service->save();
dispatch(
/**
* @throws SSHCommandError
*/
function () use ($service, $input) {
function () use ($service, $input): void {
/** @var PHP $handler */
$handler = $service->handler();
$handler->installExtension($input['extension']);
})->catch(function () use ($service, $input) {
})->catch(function () use ($service, $input): void {
$service->refresh();
$typeData = $service->type_data;
$typeData['extensions'] = array_values(array_diff($typeData['extensions'], [$input['extension']]));
@ -47,6 +48,9 @@ function () use ($service, $input) {
return $service;
}
/**
* @return array<string, array<string>>
*/
public static function rules(Server $server): array
{
return [

View File

@ -10,6 +10,11 @@
class UninstallPHP
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function uninstall(Server $server, array $input): void
{
$this->validate($server, $input);
@ -19,16 +24,18 @@ public function uninstall(Server $server, array $input): void
$php->status = ServiceStatus::UNINSTALLING;
$php->save();
dispatch(function () use ($php) {
dispatch(function () use ($php): void {
$php->handler()->uninstall();
$php->delete();
})->catch(function () use ($php) {
})->catch(function () use ($php): void {
$php->status = ServiceStatus::FAILED;
$php->save();
})->onConnection('ssh');
}
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
private function validate(Server $server, array $input): void

View File

@ -4,6 +4,7 @@
use App\Enums\PHPIniType;
use App\Models\Server;
use App\Models\Service;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
@ -14,10 +15,13 @@
class UpdatePHPIni
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function update(Server $server, array $input): void
{
/** @var Service $service */
$service = $server->php($input['version']);
$tmpName = Str::random(10).strtotime('now');
@ -48,6 +52,9 @@ private function deleteTempFile(string $name): void
}
}
/**
* @return array<string, array<string>>
*/
public static function rules(Server $server): array
{
return [

View File

@ -9,6 +9,9 @@
class AddUser
{
/**
* @param array<string, mixed> $input
*/
public function add(Project $project, array $input): void
{
/** @var User $user */
@ -18,13 +21,16 @@ public function add(Project $project, array $input): void
$project->users()->attach($user);
}
/**
* @return array<string, array<string>>
*/
public static function rules(Project $project): array
{
return [
'user' => [
'required',
Rule::exists('users', 'id'),
Rule::unique('user_project', 'user_id')->where(function (Builder $query) use ($project) {
Rule::unique('user_project', 'user_id')->where(function (Builder $query) use ($project): void {
$query->where('project_id', $project->id);
}),
],

View File

@ -8,10 +8,13 @@
class CreateProject
{
/**
* @param array<string, mixed> $input
*/
public function create(User $user, array $input): Project
{
if (isset($input['name'])) {
$input['name'] = strtolower($input['name']);
$input['name'] = strtolower((string) $input['name']);
}
$this->validate($input);
@ -27,6 +30,9 @@ public function create(User $user, array $input): Project
return $project;
}
/**
* @return array<string, array<string>>
*/
public static function rules(): array
{
return [
@ -40,6 +46,9 @@ public static function rules(): array
];
}
/**
* @param array<string, mixed> $input
*/
private function validate(array $input): void
{
Validator::make($input, self::rules())->validate();

View File

@ -8,10 +8,13 @@
class UpdateProject
{
/**
* @param array<string, mixed> $input
*/
public function update(Project $project, array $input): Project
{
if (isset($input['name'])) {
$input['name'] = strtolower($input['name']);
$input['name'] = strtolower((string) $input['name']);
}
$this->validate($project, $input);
@ -23,6 +26,9 @@ public function update(Project $project, array $input): Project
return $project;
}
/**
* @return array<string, array<string>>
*/
public static function rules(Project $project): array
{
return [
@ -36,6 +42,9 @@ public static function rules(Project $project): array
];
}
/**
* @param array<string, mixed> $input
*/
private function validate(Project $project, array $input): void
{
Validator::make($input, self::rules($project))->validate();

View File

@ -5,6 +5,7 @@
use App\Enums\QueueStatus;
use App\Models\Queue;
use App\Models\Server;
use App\Models\Service;
use App\Models\Site;
use App\SSH\Services\ProcessManager\ProcessManager;
use Illuminate\Validation\Rule;
@ -13,6 +14,9 @@
class CreateQueue
{
/**
* @param Server|Site $queueable
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function create(mixed $queueable, array $input): void
@ -29,9 +33,11 @@ public function create(mixed $queueable, array $input): void
]);
$queue->save();
dispatch(function () use ($queue) {
dispatch(function () use ($queue): void {
/** @var Service $service */
$service = $queue->server->processManager();
/** @var ProcessManager $processManager */
$processManager = $queue->server->processManager()->handler();
$processManager = $service->handler();
$processManager->create(
$queue->id,
$queue->command,
@ -44,11 +50,14 @@ public function create(mixed $queueable, array $input): void
);
$queue->status = QueueStatus::RUNNING;
$queue->save();
})->catch(function () use ($queue) {
})->catch(function () use ($queue): void {
$queue->delete();
})->onConnection('ssh');
}
/**
* @return array<string, array<string>>
*/
public static function rules(Site $site): array
{
return [

View File

@ -5,6 +5,7 @@
use App\Enums\QueueStatus;
use App\Models\Queue;
use App\Models\Server;
use App\Models\Service;
use App\SSH\Services\ProcessManager\ProcessManager;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
@ -12,6 +13,8 @@
class EditQueue
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function edit(Queue $queue, array $input): void
@ -26,9 +29,11 @@ public function edit(Queue $queue, array $input): void
]);
$queue->save();
dispatch(function () use ($queue) {
dispatch(function () use ($queue): void {
/** @var Service $service */
$service = $queue->server->processManager();
/** @var ProcessManager $processManager */
$processManager = $queue->server->processManager()->handler();
$processManager = $service->handler();
$processManager->delete($queue->id, $queue->site_id);
$processManager->create(
@ -43,12 +48,15 @@ public function edit(Queue $queue, array $input): void
);
$queue->status = QueueStatus::RUNNING;
$queue->save();
})->catch(function () use ($queue) {
})->catch(function () use ($queue): void {
$queue->status = QueueStatus::FAILED;
$queue->save();
})->onConnection('ssh');
}
/**
* @return array<string, array<string>>
*/
public static function rules(Server $server): array
{
return [

View File

@ -3,11 +3,19 @@
namespace App\Actions\Queue;
use App\Models\Queue;
use App\Models\Service;
use App\SSH\Services\ProcessManager\ProcessManager;
class GetQueueLogs
{
public function getLogs(Queue $queue): string
{
return $queue->server->processManager()->handler()->getLogs($queue->user, $queue->getLogFile());
/** @var Service $service */
$service = $queue->server->processManager();
/** @var ProcessManager $handler */
$handler = $service->handler();
return $handler->getLogs($queue->user, $queue->getLogFile());
}
}

View File

@ -4,6 +4,8 @@
use App\Enums\QueueStatus;
use App\Models\Queue;
use App\Models\Service;
use App\SSH\Services\ProcessManager\ProcessManager;
class ManageQueue
{
@ -11,8 +13,12 @@ public function start(Queue $queue): void
{
$queue->status = QueueStatus::STARTING;
$queue->save();
dispatch(function () use ($queue) {
$queue->server->processManager()->handler()->start($queue->id, $queue->site_id);
dispatch(function () use ($queue): void {
/** @var Service $service */
$service = $queue->server->processManager();
/** @var ProcessManager $handler */
$handler = $service->handler();
$handler->start($queue->id, $queue->site_id);
$queue->status = QueueStatus::RUNNING;
$queue->save();
})->onConnection('ssh');
@ -22,8 +28,12 @@ public function stop(Queue $queue): void
{
$queue->status = QueueStatus::STOPPING;
$queue->save();
dispatch(function () use ($queue) {
$queue->server->processManager()->handler()->stop($queue->id, $queue->site_id);
dispatch(function () use ($queue): void {
/** @var Service $service */
$service = $queue->server->processManager();
/** @var ProcessManager $handler */
$handler = $service->handler();
$handler->stop($queue->id, $queue->site_id);
$queue->status = QueueStatus::STOPPED;
$queue->save();
})->onConnection('ssh');
@ -33,8 +43,12 @@ public function restart(Queue $queue): void
{
$queue->status = QueueStatus::RESTARTING;
$queue->save();
dispatch(function () use ($queue) {
$queue->server->processManager()->handler()->restart($queue->id, $queue->site_id);
dispatch(function () use ($queue): void {
/** @var Service $service */
$service = $queue->server->processManager();
/** @var ProcessManager $handler */
$handler = $service->handler();
$handler->restart($queue->id, $queue->site_id);
$queue->status = QueueStatus::RUNNING;
$queue->save();
})->onConnection('ssh');

View File

@ -5,6 +5,7 @@
use App\Enums\SslStatus;
use App\Enums\SslType;
use App\Models\ServerLog;
use App\Models\Service;
use App\Models\Site;
use App\Models\Ssl;
use App\SSH\Services\Webserver\Webserver;
@ -14,6 +15,8 @@
class CreateSSL
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function create(Site $site, array $input): void
@ -40,19 +43,25 @@ public function create(Site $site, array $input): void
$ssl->log_id = ServerLog::log($site->server, 'create-ssl', '', $site)->id;
$ssl->save();
dispatch(function () use ($site, $ssl) {
dispatch(function () use ($site, $ssl): void {
/** @var Service $service */
$service = $site->server->webserver();
/** @var Webserver $webserver */
$webserver = $site->server->webserver()->handler();
$webserver = $service->handler();
$webserver->setupSSL($ssl);
$ssl->status = SslStatus::CREATED;
$ssl->save();
$webserver->updateVHost($site);
})->catch(function () use ($ssl) {
})->catch(function () use ($ssl): void {
$ssl->status = SslStatus::FAILED;
$ssl->save();
})->onConnection('ssh');
}
/**
* @param array<string, mixed> $input
* @return array<string, mixed>
*/
public static function rules(array $input): array
{
$rules = [

View File

@ -3,6 +3,7 @@
namespace App\Actions\SSL;
use App\Enums\SslStatus;
use App\Models\Service;
use App\Models\Ssl;
use App\SSH\Services\Webserver\Webserver;
@ -12,8 +13,10 @@ public function delete(Ssl $ssl): void
{
$ssl->status = SslStatus::DELETING;
$ssl->save();
/** @var Service $service */
$service = $ssl->site->server->webserver();
/** @var Webserver $webserver */
$webserver = $ssl->site->server->webserver()->handler();
$webserver = $service->handler();
$webserver->removeSSL($ssl);
$ssl->delete();
}

View File

@ -7,6 +7,9 @@
class CreateScript
{
/**
* @param array<string, mixed> $input
*/
public function create(User $user, array $input): Script
{
$script = new Script([
@ -20,6 +23,9 @@ public function create(User $user, array $input): Script
return $script;
}
/**
* @return array<string, array<string>>
*/
public static function rules(): array
{
return [

View File

@ -7,6 +7,9 @@
class EditScript
{
/**
* @param array<string, mixed> $input
*/
public function edit(Script $script, User $user, array $input): Script
{
$script->name = $input['name'];
@ -18,6 +21,9 @@ public function edit(Script $script, User $user, array $input): Script
return $script;
}
/**
* @return array<string, array<string>>
*/
public static function rules(): array
{
return [

View File

@ -11,6 +11,9 @@
class ExecuteScript
{
/**
* @param array<string, mixed> $input
*/
public function execute(Script $script, array $input): ScriptExecution
{
$execution = new ScriptExecution([
@ -22,16 +25,19 @@ public function execute(Script $script, array $input): ScriptExecution
]);
$execution->save();
dispatch(function () use ($execution, $script) {
dispatch(function () use ($execution, $script): void {
/** @var Server $server */
$server = $execution->server;
$content = $execution->getContent();
$log = ServerLog::make($execution->server, 'script-'.$script->id.'-'.strtotime('now'));
$log = ServerLog::newLog($server, 'script-'.$script->id.'-'.strtotime('now'));
$log->save();
$execution->server_log_id = $log->id;
$execution->save();
$execution->server->os()->runScript('~/', $content, $log, $execution->user);
$server->os()->runScript('~/', $content, $log, $execution->user);
$execution->status = ScriptExecutionStatus::COMPLETED;
$execution->save();
})->catch(function () use ($execution) {
})->catch(function () use ($execution): void {
$execution->status = ScriptExecutionStatus::FAILED;
$execution->save();
})->onConnection('ssh');
@ -39,12 +45,16 @@ public function execute(Script $script, array $input): ScriptExecution
return $execution;
}
/**
* @param array<string, mixed> $input
* @return array<string, mixed>
*/
public static function rules(array $input): array
{
$users = ['root'];
if (isset($input['server'])) {
/** @var ?Server $server */
$server = Server::query()->find($input['server']);
/** @var Server $server */
$server = Server::query()->findOrFail($input['server']);
$users = $server->getSshUsers();
}

View File

@ -24,6 +24,9 @@
class CreateServer
{
/**
* @param array<string, mixed> $input
*/
public function create(User $creator, Project $project, array $input): Server
{
$server = new Server([
@ -81,7 +84,7 @@ public function create(User $creator, Project $project, array $input): Server
private function install(Server $server): void
{
dispatch(function () use ($server) {
dispatch(function () use ($server): void {
$maxWait = 180;
while ($maxWait > 0) {
sleep(10);
@ -102,7 +105,7 @@ private function install(Server $server): void
]);
Notifier::send($server, new ServerInstallationSucceed($server));
})
->catch(function (Throwable $e) use ($server) {
->catch(function (Throwable $e) use ($server): void {
$server->update([
'status' => ServerStatus::INSTALLATION_FAILED,
]);
@ -114,6 +117,10 @@ private function install(Server $server): void
->onConnection('ssh');
}
/**
* @param array<string, mixed> $input
* @return array<string, mixed>
*/
public static function rules(Project $project, array $input): array
{
$rules = [
@ -129,28 +136,22 @@ public static function rules(Project $project, array $input): array
Rule::in(config('core.operating_systems')),
],
'server_provider' => [
Rule::when(function () use ($input) {
return isset($input['provider']) && $input['provider'] != ServerProvider::CUSTOM;
}, [
Rule::when(fn (): bool => isset($input['provider']) && $input['provider'] != ServerProvider::CUSTOM, [
'required',
Rule::exists('server_providers', 'id')->where(function (Builder $query) use ($project) {
Rule::exists('server_providers', 'id')->where(function (Builder $query) use ($project): void {
$query->where('project_id', $project->id)
->orWhereNull('project_id');
}),
]),
],
'ip' => [
Rule::when(function () use ($input) {
return isset($input['provider']) && $input['provider'] == ServerProvider::CUSTOM;
}, [
Rule::when(fn (): bool => isset($input['provider']) && $input['provider'] == ServerProvider::CUSTOM, [
'required',
new RestrictedIPAddressesRule,
]),
],
'port' => [
Rule::when(function () use ($input) {
return isset($input['provider']) && $input['provider'] == ServerProvider::CUSTOM;
}, [
Rule::when(fn (): bool => isset($input['provider']) && $input['provider'] == ServerProvider::CUSTOM, [
'required',
'numeric',
'min:1',
@ -162,6 +163,10 @@ public static function rules(Project $project, array $input): array
return array_merge($rules, self::typeRules($input), self::providerRules($input));
}
/**
* @param array<string, mixed> $input
* @return array<string, array<string>>
*/
private static function typeRules(array $input): array
{
if (! isset($input['type']) || ! in_array($input['type'], config('core.server_types'))) {
@ -173,6 +178,10 @@ private static function typeRules(array $input): array
return $server->type()->createRules($input);
}
/**
* @param array<string, mixed> $input
* @return array<string, array<string>>
*/
private static function providerRules(array $input): array
{
if (

View File

@ -8,6 +8,8 @@
class CreateServerLog
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function create(Server $server, array $input): void
@ -20,6 +22,9 @@ public function create(Server $server, array $input): void
]);
}
/**
* @return array<string, string>
*/
public static function rules(): array
{
return [

View File

@ -10,6 +10,9 @@
class EditServer
{
/**
* @param array<string, mixed> $input
* @return Server $server
*
* @throws ValidationException
*/
public function edit(Server $server, array $input): Server
@ -42,6 +45,9 @@ public function edit(Server $server, array $input): Server
return $server;
}
/**
* @return array<string, array<int, mixed>>
*/
public static function rules(Server $server): array
{
return [

View File

@ -13,11 +13,11 @@ public function update(Server $server): void
{
$server->status = ServerStatus::UPDATING;
$server->save();
dispatch(function () use ($server) {
dispatch(function () use ($server): void {
$server->os()->upgrade();
$server->checkConnection();
$server->checkForUpdates();
})->catch(function () use ($server) {
})->catch(function () use ($server): void {
Notifier::send($server, new ServerUpdateFailed($server));
$server->checkConnection();
})->onConnection('ssh');

View File

@ -3,6 +3,7 @@
namespace App\Actions\ServerProvider;
use App\Models\Project;
use App\Models\Server;
use App\Models\ServerProvider;
use App\Models\User;
use App\ServerProviders\ServerProvider as ServerProviderContract;
@ -13,11 +14,13 @@
class CreateServerProvider
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function create(User $user, Project $project, array $input): ServerProvider
{
$provider = static::getProvider($input['provider']);
$provider = self::getProvider($input['provider']);
try {
$provider->connect($input);
@ -40,13 +43,19 @@ public function create(User $user, Project $project, array $input): ServerProvid
return $serverProvider;
}
private static function getProvider($name): ServerProviderContract
private static function getProvider(string $name): ServerProviderContract
{
$providerClass = config('core.server_providers_class.'.$name);
/** @var ServerProviderContract $provider */
$provider = new $providerClass(new ServerProvider, new Server);
return new $providerClass;
return $provider;
}
/**
* @param array<string, mixed> $input
* @return array<string, mixed>
*/
public static function rules(array $input): array
{
$rules = [
@ -60,15 +69,19 @@ public static function rules(array $input): array
],
];
return array_merge($rules, static::providerRules($input));
return array_merge($rules, self::providerRules($input));
}
/**
* @param array<string, mixed> $input
* @return array<string, array<string>>
*/
private static function providerRules(array $input): array
{
if (! isset($input['provider'])) {
return [];
}
return static::getProvider($input['provider'])->credentialValidationRules($input);
return self::getProvider($input['provider'])->credentialValidationRules($input);
}
}

View File

@ -7,6 +7,9 @@
class EditServerProvider
{
/**
* @param array<string, mixed> $input
*/
public function edit(ServerProvider $serverProvider, Project $project, array $input): ServerProvider
{
$serverProvider->profile = $input['name'];
@ -17,6 +20,9 @@ public function edit(ServerProvider $serverProvider, Project $project, array $in
return $serverProvider;
}
/**
* @return array<string, array<string>>
*/
public static function rules(): array
{
return [

View File

@ -10,6 +10,9 @@
class Install
{
/**
* @param array<string, mixed> $input
*/
public function install(Server $server, array $input): Service
{
$input['type'] = config('core.service_types')[$input['name']];
@ -28,11 +31,11 @@ public function install(Server $server, array $input): Service
$service->save();
dispatch(function () use ($service) {
dispatch(function () use ($service): void {
$service->handler()->install();
$service->status = ServiceStatus::READY;
$service->save();
})->catch(function () use ($service) {
})->catch(function () use ($service): void {
$service->status = ServiceStatus::INSTALLATION_FAILED;
$service->save();
})->onConnection('ssh');
@ -40,6 +43,10 @@ public function install(Server $server, array $input): Service
return $service;
}
/**
* @param array<string, mixed> $input
* @return array<string, array<int, mixed>>
*/
public static function rules(array $input): array
{
$rules = [

View File

@ -11,7 +11,7 @@ public function start(Service $service): void
{
$service->status = ServiceStatus::STARTING;
$service->save();
dispatch(function () use ($service) {
dispatch(function () use ($service): void {
$status = $service->server->systemd()->start($service->unit);
if (str($status)->contains('Active: active')) {
$service->status = ServiceStatus::READY;
@ -26,7 +26,7 @@ public function stop(Service $service): void
{
$service->status = ServiceStatus::STOPPING;
$service->save();
dispatch(function () use ($service) {
dispatch(function () use ($service): void {
$status = $service->server->systemd()->stop($service->unit);
if (str($status)->contains('Active: inactive')) {
$service->status = ServiceStatus::STOPPED;
@ -41,7 +41,7 @@ public function restart(Service $service): void
{
$service->status = ServiceStatus::RESTARTING;
$service->save();
dispatch(function () use ($service) {
dispatch(function () use ($service): void {
$status = $service->server->systemd()->restart($service->unit);
if (str($status)->contains('Active: active')) {
$service->status = ServiceStatus::READY;
@ -56,7 +56,7 @@ public function enable(Service $service): void
{
$service->status = ServiceStatus::ENABLING;
$service->save();
dispatch(function () use ($service) {
dispatch(function () use ($service): void {
$status = $service->server->systemd()->enable($service->unit);
if (str($status)->contains('Active: active')) {
$service->status = ServiceStatus::READY;
@ -71,7 +71,7 @@ public function disable(Service $service): void
{
$service->status = ServiceStatus::DISABLING;
$service->save();
dispatch(function () use ($service) {
dispatch(function () use ($service): void {
$status = $service->server->systemd()->disable($service->unit);
if (str($status)->contains('Active: inactive')) {
$service->status = ServiceStatus::DISABLED;

View File

@ -20,10 +20,10 @@ public function uninstall(Service $service): void
$service->status = ServiceStatus::UNINSTALLING;
$service->save();
dispatch(function () use ($service) {
dispatch(function () use ($service): void {
$service->handler()->uninstall();
$service->delete();
})->catch(function () use ($service) {
})->catch(function () use ($service): void {
$service->status = ServiceStatus::FAILED;
$service->save();
})->onConnection('ssh');

View File

@ -7,6 +7,9 @@
class CreateCommand
{
/**
* @param array<string, mixed> $input
*/
public function create(Site $site, array $input): Command
{
$script = new Command([
@ -19,6 +22,9 @@ public function create(Site $site, array $input): Command
return $script;
}
/**
* @return array<string, array<string>>
*/
public static function rules(): array
{
return [

View File

@ -19,6 +19,11 @@
class CreateSite
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function create(Server $server, array $input): Site
{
DB::beginTransaction();
@ -40,7 +45,7 @@ public function create(Server $server, array $input): Site
// check has access to repository
try {
if ($site->sourceControl) {
$site->sourceControl?->getRepo($site->repository);
$site->sourceControl->getRepo($site->repository);
}
} catch (SourceControlIsNotConnected) {
throw ValidationException::withMessages([
@ -72,14 +77,14 @@ public function create(Server $server, array $input): Site
$site->commands()->createMany($site->type()->baseCommands());
// install site
dispatch(function () use ($site) {
dispatch(function () use ($site): void {
$site->type()->install();
$site->update([
'status' => SiteStatus::READY,
'progress' => 100,
]);
Notifier::send($site, new SiteInstallationSucceed($site));
})->catch(function () use ($site) {
})->catch(function () use ($site): void {
$site->status = SiteStatus::INSTALLATION_FAILED;
$site->save();
Notifier::send($site, new SiteInstallationFailed($site));
@ -96,6 +101,10 @@ public function create(Server $server, array $input): Site
}
}
/**
* @param array<string, mixed> $input
* @return array<string, mixed>
*/
public static function rules(Server $server, array $input): array
{
$rules = [
@ -106,9 +115,7 @@ public static function rules(Server $server, array $input): array
'domain' => [
'required',
new DomainRule,
Rule::unique('sites', 'domain')->where(function ($query) use ($server) {
return $query->where('server_id', $server->id);
}),
Rule::unique('sites', 'domain')->where(fn ($query) => $query->where('server_id', $server->id)),
],
'aliases.*' => [
new DomainRule,
@ -125,6 +132,10 @@ public static function rules(Server $server, array $input): array
return array_merge($rules, self::typeRules($server, $input));
}
/**
* @param array<string, mixed> $input
* @return array<string, array<string>>
*/
private static function typeRules(Server $server, array $input): array
{
if (! isset($input['type']) || ! in_array($input['type'], config('core.site_types'))) {

View File

@ -3,6 +3,7 @@
namespace App\Actions\Site;
use App\Exceptions\SSHError;
use App\Models\Service;
use App\Models\Site;
use App\SSH\Services\PHP\PHP;
use App\SSH\Services\Webserver\Webserver;
@ -14,13 +15,18 @@ class DeleteSite
*/
public function delete(Site $site): void
{
/** @var Service $service */
$service = $site->server->webserver();
/** @var Webserver $webserverHandler */
$webserverHandler = $site->server->webserver()->handler();
$webserverHandler = $service->handler();
$webserverHandler->deleteSite($site);
if ($site->isIsolated()) {
/** @var Service $phpService */
$phpService = $site->server->php();
/** @var PHP $php */
$php = $site->server->php()->handler();
$php = $phpService->handler();
$php->removeFpmPool($site->user, $site->php_version, $site->id);
$os = $site->server->os();

View File

@ -39,9 +39,8 @@ public function run(Site $site): Deployment
}
$deployment->save();
dispatch(function () use ($site, $deployment) {
/** @var ServerLog $log */
$log = ServerLog::make($site->server, 'deploy-'.strtotime('now'))
dispatch(function () use ($site, $deployment): void {
$log = ServerLog::newLog($site->server, 'deploy-'.strtotime('now'))
->forSite($site);
$log->save();
$deployment->log_id = $log->id;
@ -56,7 +55,7 @@ public function run(Site $site): Deployment
$deployment->status = DeploymentStatus::FINISHED;
$deployment->save();
Notifier::send($site, new DeploymentCompleted($deployment, $site));
})->catch(function () use ($deployment, $site) {
})->catch(function () use ($deployment, $site): void {
$deployment->status = DeploymentStatus::FAILED;
$deployment->save();
Notifier::send($site, new DeploymentCompleted($deployment, $site));

View File

@ -6,6 +6,9 @@
class EditCommand
{
/**
* @param array<string, mixed> $input
*/
public function edit(Command $command, array $input): Command
{
$command->name = $input['name'];
@ -15,6 +18,9 @@ public function edit(Command $command, array $input): Command
return $command;
}
/**
* @return array<string, array<string>>
*/
public static function rules(): array
{
return [

View File

@ -10,6 +10,9 @@
class ExecuteCommand
{
/**
* @param array<string, mixed> $input
*/
public function execute(Command $command, User $user, array $input): CommandExecution
{
$execution = new CommandExecution([
@ -21,22 +24,22 @@ public function execute(Command $command, User $user, array $input): CommandExec
]);
$execution->save();
dispatch(function () use ($execution, $command) {
dispatch(function () use ($execution, $command): void {
$content = $execution->getContent();
$log = ServerLog::make($execution->server, 'command-'.$command->id.'-'.strtotime('now'));
$log = ServerLog::newLog($execution->server, 'command-'.$command->id.'-'.strtotime('now'));
$log->save();
$execution->server_log_id = $log->id;
$execution->save();
$execution->server->os()->runScript(
path: $command->site->path,
script: $content,
user: $command->site->user,
serverLog: $log,
user: $command->site->user,
variables: $execution->variables
);
$execution->status = CommandExecutionStatus::COMPLETED;
$execution->save();
})->catch(function () use ($execution) {
})->catch(function () use ($execution): void {
$execution->status = CommandExecutionStatus::FAILED;
$execution->save();
})->onConnection('ssh');
@ -44,6 +47,10 @@ public function execute(Command $command, User $user, array $input): CommandExec
return $execution;
}
/**
* @param array<string, mixed> $input
* @return array<string, string|array<int, mixed>>
*/
public static function rules(array $input): array
{
return [

View File

@ -2,23 +2,33 @@
namespace App\Actions\Site;
use App\Models\Service;
use App\Models\Site;
use App\SSH\Services\Webserver\Webserver;
use App\ValidationRules\DomainRule;
class UpdateAliases
{
/**
* @param array<string, mixed> $input
*/
public function update(Site $site, array $input): void
{
$site->aliases = $input['aliases'] ?? [];
/** @var Service $service */
$service = $site->server->webserver();
/** @var Webserver $webserver */
$webserver = $site->server->webserver()->handler();
$webserver = $service->handler();
$webserver->updateVHost($site);
$site->save();
}
/**
* @return array<string, array<int, mixed>>
*/
public static function rules(): array
{
return [

View File

@ -5,12 +5,12 @@
use App\Exceptions\SSHError;
use App\Models\Site;
use App\SSH\Git\Git;
use Illuminate\Validation\ValidationException;
class UpdateBranch
{
/**
* @throws ValidationException
* @param array<string, mixed> $input
*
* @throws SSHError
*/
public function update(Site $site, array $input): void
@ -22,7 +22,7 @@ public function update(Site $site, array $input): void
}
/**
* @throws ValidationException
* @return array<string, string>
*/
public static function rules(): array
{

View File

@ -2,23 +2,24 @@
namespace App\Actions\Site;
use App\Models\DeploymentScript;
use App\Models\Site;
use Illuminate\Validation\ValidationException;
class UpdateDeploymentScript
{
/**
* @throws ValidationException
* @param array<string, mixed> $input
*/
public function update(Site $site, array $input): void
{
/** @var DeploymentScript $script */
$script = $site->deploymentScript;
$script->content = $input['script'];
$script->save();
}
/**
* @throws ValidationException
* @return array<string, array<string>>
*/
public static function rules(): array
{

View File

@ -8,6 +8,8 @@
class UpdateEnv
{
/**
* @param array<string, mixed> $input
*
* @throws SSHError
*/
public function update(Site $site, array $input): void
@ -15,7 +17,7 @@ public function update(Site $site, array $input): void
$site->server->os()->editFileAs(
$site->path.'/.env',
$site->user,
trim($input['env']),
trim((string) $input['env']),
);
}
}

View File

@ -9,6 +9,9 @@
class UpdateLoadBalancer
{
/**
* @param array<string, mixed> $input
*/
public function update(Site $site, array $input): void
{
$site->loadBalancerServers()->delete();
@ -27,6 +30,9 @@ public function update(Site $site, array $input): void
$site->webserver()->updateVHost($site);
}
/**
* @return array<string, array<int, mixed>>
*/
public static function rules(Site $site): array
{
return [

View File

@ -8,6 +8,9 @@
class UpdatePHPVersion
{
/**
* @return array<string, array<string>>
*/
public static function rules(Site $site): array
{
return [
@ -21,6 +24,8 @@ public static function rules(Site $site): array
}
/**
* @param array<string, mixed> $input
*
* @throws SSHError
*/
public function update(Site $site, array $input): void

View File

@ -11,12 +11,17 @@
class UpdateSourceControl
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function update(Site $site, array $input): void
{
$site->source_control_id = $input['source_control'];
try {
if ($site->sourceControl) {
$site->sourceControl?->getRepo($site->repository);
$site->sourceControl->getRepo($site->repository);
}
} catch (SourceControlIsNotConnected) {
throw ValidationException::withMessages([
@ -34,6 +39,9 @@ public function update(Site $site, array $input): void
$site->save();
}
/**
* @return array<string, array<int, mixed>>
*/
public static function rules(): array
{
return [

View File

@ -4,14 +4,18 @@
use App\Models\Project;
use App\Models\SourceControl;
use App\Models\User;
use Illuminate\Support\Arr;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
class ConnectSourceControl
{
public function connect(User $user, Project $project, array $input): SourceControl
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function connect(Project $project, array $input): SourceControl
{
$sourceControl = new SourceControl([
'provider' => $input['provider'],
@ -34,6 +38,10 @@ public function connect(User $user, Project $project, array $input): SourceContr
return $sourceControl;
}
/**
* @param array<string, mixed> $input
* @return array<string, array<int, mixed>>
*/
public static function rules(array $input): array
{
$rules = [
@ -46,10 +54,13 @@ public static function rules(array $input): array
],
];
return array_merge($rules, static::providerRules($input));
return array_merge($rules, self::providerRules($input));
}
/**
* @param array<string, mixed> $input
* @return array<string, array<string>>
*
* @throws ValidationException
*/
private static function providerRules(array $input): array

View File

@ -8,6 +8,11 @@
class EditSourceControl
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function edit(SourceControl $sourceControl, Project $project, array $input): SourceControl
{
$sourceControl->profile = $input['name'];
@ -27,6 +32,10 @@ public function edit(SourceControl $sourceControl, Project $project, array $inpu
return $sourceControl;
}
/**
* @param array<string, mixed> $input
* @return array<string, array<int, mixed>>
*/
public static function rules(SourceControl $sourceControl, array $input): array
{
$rules = [
@ -35,10 +44,13 @@ public static function rules(SourceControl $sourceControl, array $input): array
],
];
return array_merge($rules, static::providerRules($sourceControl, $input));
return array_merge($rules, self::providerRules($sourceControl, $input));
}
/**
* @param array<string, mixed> $input
* @return array<string, array<int, mixed>>
*
* @throws ValidationException
*/
private static function providerRules(SourceControl $sourceControl, array $input): array

View File

@ -10,6 +10,8 @@
class CreateSshKey
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function create(User $user, array $input): SshKey
@ -25,7 +27,7 @@ public function create(User $user, array $input): SshKey
}
/**
* @throws ValidationException
* @return array<string, mixed>
*/
public static function rules(): array
{

View File

@ -3,11 +3,15 @@
namespace App\Actions\SshKey;
use App\Enums\SshKeyStatus;
use App\Exceptions\SSHError;
use App\Models\Server;
use App\Models\SshKey;
class DeleteKeyFromServer
{
/**
* @throws SSHError
*/
public function delete(Server $server, SshKey $sshKey): void
{
$sshKey->servers()->updateExistingPivot($server->id, [

View File

@ -3,6 +3,7 @@
namespace App\Actions\SshKey;
use App\Enums\SshKeyStatus;
use App\Exceptions\SSHError;
use App\Models\Server;
use App\Models\SshKey;
use App\Models\User;
@ -10,6 +11,11 @@
class DeployKeyToServer
{
/**
* @param array<string, mixed> $input
*
* @throws SSHError
*/
public function deploy(Server $server, array $input): void
{
/** @var SshKey $sshKey */
@ -23,6 +29,9 @@ public function deploy(Server $server, array $input): void
]);
}
/**
* @return array<string, array<string>>
*/
public static function rules(User $user, Server $server): array
{
return [

View File

@ -11,6 +11,8 @@
class CreateStorageProvider
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function create(User $user, Project $project, array $input): StorageProvider
@ -41,6 +43,10 @@ public function create(User $user, Project $project, array $input): StorageProvi
return $storageProvider;
}
/**
* @param array<string, mixed> $input
* @return array<string, mixed>
*/
public static function rules(array $input): array
{
$rules = [

View File

@ -4,10 +4,12 @@
use App\Models\Project;
use App\Models\StorageProvider;
use Illuminate\Validation\ValidationException;
class EditStorageProvider
{
/**
* @param array<string, mixed> $input
*/
public function edit(StorageProvider $storageProvider, Project $project, array $input): StorageProvider
{
$storageProvider->profile = $input['name'];
@ -19,7 +21,7 @@ public function edit(StorageProvider $storageProvider, Project $project, array $
}
/**
* @throws ValidationException
* @return array<string, mixed>
*/
public static function rules(): array
{

View File

@ -9,6 +9,11 @@
class CreateTag
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function create(User $user, array $input): Tag
{
$tag = Tag::query()
@ -22,7 +27,7 @@ public function create(User $user, array $input): Tag
}
$tag = new Tag([
'project_id' => $user->currentProject->id,
'project_id' => $user->currentProject?->id,
'name' => $input['name'],
'color' => $input['color'],
]);
@ -31,6 +36,9 @@ public function create(User $user, array $input): Tag
return $tag;
}
/**
* @return array<string, mixed>
*/
public static function rules(): array
{
return [

View File

@ -7,6 +7,9 @@
class EditTag
{
/**
* @param array<string, mixed> $input
*/
public function edit(Tag $tag, array $input): void
{
$tag->name = $input['name'];
@ -15,6 +18,9 @@ public function edit(Tag $tag, array $input): void
$tag->save();
}
/**
* @return array<string, array<string>>
*/
public static function rules(): array
{
return [

View File

@ -5,12 +5,14 @@
use App\Models\Server;
use App\Models\Site;
use App\Models\Tag;
use App\Models\User;
use Illuminate\Validation\Rule;
class SyncTags
{
public function sync(User $user, array $input): void
/**
* @param array<string, mixed> $input
*/
public function sync(array $input): void
{
/** @var Server|Site $taggable */
$taggable = $input['taggable_type']::findOrFail($input['taggable_id']);
@ -20,6 +22,9 @@ public function sync(User $user, array $input): void
$taggable->tags()->sync($tags->pluck('id'));
}
/**
* @return array<string, array<string>>
*/
public static function rules(int $projectId): array
{
return [

View File

@ -9,6 +9,9 @@
class CreateUser
{
/**
* @param array<string, mixed> $input
*/
public function create(array $input): User
{
$this->validate($input);
@ -25,11 +28,17 @@ public function create(array $input): User
return $user;
}
/**
* @param array<string, mixed> $input
*/
private function validate(array $input): void
{
Validator::make($input, self::rules())->validate();
}
/**
* @return array<string, mixed>
*/
public static function rules(): array
{
return [

View File

@ -7,9 +7,7 @@
trait PasswordValidationRules
{
/**
* Get the validation rules used to validate passwords.
*
* @return array<int, \Illuminate\Contracts\Validation\Rule|array|string>
* @return array<int, mixed>
*/
protected function passwordRules(): array
{

View File

@ -5,9 +5,15 @@
use App\Models\Project;
use App\Models\User;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
class UpdateProjects
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function update(User $user, array $input): void
{
$this->validate($input);
@ -20,7 +26,7 @@ public function update(User $user, array $input): void
$user->refresh();
/** @var Project $firstProject */
/** @var ?Project $firstProject */
$firstProject = $user->projects->first();
if (! $user->currentProject && $firstProject) {
$user->current_project_id = $firstProject->id;
@ -28,11 +34,19 @@ public function update(User $user, array $input): void
}
}
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
private function validate(array $input): void
{
validator($input, self::rules())->validate();
}
/**
* @return array<string, array<string>>
*/
public static function rules(): array
{
return [

View File

@ -9,6 +9,9 @@
class UpdateUser
{
/**
* @param array<string, mixed> $input
*/
public function update(User $user, array $input): User
{
$this->validate($user, $input);
@ -27,11 +30,17 @@ public function update(User $user, array $input): User
return $user;
}
/**
* @param array<string, mixed> $input
*/
private function validate(User $user, array $input): void
{
Validator::make($input, self::rules($user))->validate();
}
/**
* @return array<string, mixed>
*/
public static function rules(User $user): array
{
return [

View File

@ -6,6 +6,10 @@
class UpdateUserPassword
{
/**
* @param mixed $user
* @param array<string, mixed> $input
*/
public function update($user, array $input): void
{
$user->forceFill([
@ -13,6 +17,9 @@ public function update($user, array $input): void
])->save();
}
/**
* @return array<string, array<string>>
*/
public static function rules(): array
{
return [

View File

@ -4,9 +4,15 @@
use App\Models\User;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
class UpdateUserProfileInformation
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function update(User $user, array $input): void
{
if ($input['email'] !== $user->email) {
@ -20,6 +26,9 @@ public function update(User $user, array $input): void
}
}
/**
* @return array<string, array<string>>
*/
public static function rules(User $user): array
{
return [
@ -33,7 +42,7 @@ public static function rules(User $user): array
}
/**
* Update the given verified user's profile information.
* @param array<string, mixed> $input
*/
protected function updateVerifiedUser(User $user, array $input): void
{

View File

@ -11,10 +11,10 @@ class DeleteOlderMetricsCommand extends Command
protected $description = 'Delete older metrics from database';
public function handle()
public function handle(): void
{
Service::query()->where('type', 'monitoring')->chunk(100, function ($services) {
$services->each(function ($service) {
Service::query()->where('type', 'monitoring')->chunk(100, function ($services): void {
$services->each(function ($service): void {
$this->info("Deleting older metrics for service {$service->server->name}");
$service
->server

View File

@ -15,10 +15,10 @@ class GetMetricsCommand extends Command
public function handle(): void
{
$checkedMetrics = 0;
Server::query()->whereHas('services', function (Builder $query) {
Server::query()->whereHas('services', function (Builder $query): void {
$query->where('type', 'monitoring')
->where('name', 'remote-monitor');
})->chunk(10, function ($servers) use (&$checkedMetrics) {
})->chunk(10, function ($servers) use (&$checkedMetrics): void {
/** @var Server $server */
foreach ($servers as $server) {
$info = $server->os()->resourceInfo();

View File

@ -16,9 +16,9 @@ public function handle(): void
{
$this->info('Migrating from Mysql to SQLite...');
File::exists(storage_path('database.sqlite'))
? File::delete(storage_path('database.sqlite'))
: null;
if (File::exists(storage_path('database.sqlite'))) {
File::delete(storage_path('database.sqlite'));
}
File::put(storage_path('database.sqlite'), '');

View File

@ -20,7 +20,7 @@ public function handle(): void
Backup::query()
->where('interval', $this->argument('interval'))
->where('status', BackupStatus::RUNNING)
->chunk(100, function ($backups) use (&$total) {
->chunk(100, function ($backups) use (&$total): void {
/** @var Backup $backup */
foreach ($backups as $backup) {
app(RunBackup::class)->run($backup);

View File

@ -43,7 +43,7 @@ class Handler extends ExceptionHandler
*/
public function register(): void
{
$this->reportable(function (Throwable $e) {
$this->reportable(function (Throwable $e): void {
//
});
}

View File

@ -8,12 +8,8 @@
class SSHError extends Exception
{
protected ?ServerLog $log;
public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null, ?ServerLog $log = null)
public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null, protected ?ServerLog $log = null)
{
$this->log = $log;
parent::__construct($message, $code, $previous);
}

View File

@ -7,7 +7,7 @@
use Illuminate\Support\Facades\Facade;
/**
* @method static bool|Connection connect(string $host, string $port, bool $ssl = false)
* @method static bool|Connection connect(string $host, int $port, bool $ssl = false)
* @method static bool login(string $username, string $password, bool|Connection $connection)
* @method static void close(bool|Connection $connection)
* @method static bool passive(bool|Connection $connection, bool $passive)

View File

@ -2,22 +2,20 @@
namespace App\Facades;
use App\Models\Server;
use App\Models\ServerLog;
use App\Support\Testing\SSHFake;
use Illuminate\Support\Facades\Facade as FacadeAlias;
/**
* Class SSH
*
* @method static init(Server $server, string $asUser = null)
* @method static setLog(?ServerLog $log)
* @method static \App\Helpers\SSH|SSHFake init(\App\Models\Server $server, string $asUser = null)
* @method static setLog(?\App\Models\ServerLog $log)
* @method static connect()
* @method static string exec(string $command, string $log = '', int $siteId = null, ?bool $stream = false, callable $streamCallback = null)
* @method static string upload(string $local, string $remote, ?string $owner = null)
* @method static string download(string $local, string $remote)
* @method static string write(string $path, string $content, string $owner = null)
* @method static string assertExecuted(array|string $commands)
* @method static string assertExecuted(array<int, string>|string $commands)
* @method static string assertExecutedContains(string $command)
* @method static string assertFileUploaded(string $toPath, ?string $content = null)
* @method static string getUploadedLocalPath()

View File

@ -59,11 +59,9 @@ class Agent extends MobileDetect
*/
public function platform()
{
return $this->retrieveUsingCacheOrResolve('paymently.platform', function () {
return $this->findDetectionRulesAgainstUserAgent(
$this->mergeRules(MobileDetect::getOperatingSystems(), static::$additionalOperatingSystems)
);
});
return $this->retrieveUsingCacheOrResolve('paymently.platform', fn () => $this->findDetectionRulesAgainstUserAgent(
$this->mergeRules(MobileDetect::getOperatingSystems(), static::$additionalOperatingSystems)
));
}
/**
@ -73,11 +71,9 @@ public function platform()
*/
public function browser()
{
return $this->retrieveUsingCacheOrResolve('paymently.browser', function () {
return $this->findDetectionRulesAgainstUserAgent(
$this->mergeRules(static::$additionalBrowsers, MobileDetect::getBrowsers())
);
});
return $this->retrieveUsingCacheOrResolve('paymently.browser', fn () => $this->findDetectionRulesAgainstUserAgent(
$this->mergeRules(static::$additionalBrowsers, MobileDetect::getBrowsers())
));
}
/**
@ -87,7 +83,7 @@ public function browser()
*/
public function isDesktop()
{
return $this->retrieveUsingCacheOrResolve('paymently.desktop', function () {
return $this->retrieveUsingCacheOrResolve('paymently.desktop', function (): bool {
// Check specifically for cloudfront headers if the useragent === 'Amazon CloudFront'
if (
$this->getUserAgent() === static::$cloudFrontUA
@ -103,19 +99,24 @@ public function isDesktop()
/**
* Match a detection rule and return the matched key.
*
* @param array<mixed, string> $rules
* @return string|null
*/
protected function findDetectionRulesAgainstUserAgent(array $rules)
{
$userAgent = $this->getUserAgent();
if ($userAgent === null || $userAgent === '' || $userAgent === '0') {
return null;
}
foreach ($rules as $key => $regex) {
if (empty($regex)) {
continue;
}
if ($this->match($regex, $userAgent)) {
return $key ?: reset($this->matchesArray);
return $key !== 0 && ($key !== '' && $key !== '0') ? $key : reset($this->matchesArray);
}
}
@ -136,7 +137,7 @@ protected function retrieveUsingCacheOrResolve(string $key, Closure $callback)
return $cacheItem;
}
return tap(call_user_func($callback), function ($result) use ($cacheKey) {
return tap(call_user_func($callback), function ($result) use ($cacheKey): void {
$this->store[$cacheKey] = $result;
});
}
@ -144,10 +145,10 @@ protected function retrieveUsingCacheOrResolve(string $key, Closure $callback)
/**
* Merge multiple rules into one array.
*
* @param array $all
* @param array<mixed> $all
* @return array<string, string>
*/
protected function mergeRules(...$all)
protected function mergeRules(...$all): array
{
$merged = [];

View File

@ -6,7 +6,7 @@
class FTP
{
public function connect(string $host, string $port, bool $ssl = false): bool|Connection
public function connect(string $host, int $port, bool $ssl = false): bool|Connection
{
if ($ssl) {
return ftp_ssl_connect($host, $port, 5);
@ -15,22 +15,22 @@ public function connect(string $host, string $port, bool $ssl = false): bool|Con
return ftp_connect($host, $port, 5);
}
public function login(string $username, string $password, bool|Connection $connection): bool
public function login(string $username, string $password, Connection $connection): bool
{
return ftp_login($connection, $username, $password);
}
public function close(bool|Connection $connection): void
public function close(Connection $connection): void
{
ftp_close($connection);
}
public function passive(bool|Connection $connection, bool $passive): bool
public function passive(Connection $connection, bool $passive): bool
{
return ftp_pasv($connection, $passive);
}
public function delete(bool|Connection $connection, string $path): bool
public function delete(Connection $connection, string $path): bool
{
return ftp_delete($connection, $path);
}

View File

@ -23,13 +23,13 @@ class SSH
{
public Server $server;
public ?ServerLog $log;
public ?ServerLog $log = null;
protected SSH2|SFTP|null $connection = null;
protected ?string $user;
protected string $user = '';
protected ?string $asUser;
protected ?string $asUser = null;
protected string $publicKey;
@ -42,11 +42,11 @@ public function init(Server $server, ?string $asUser = null): self
$this->asUser = null;
$this->server = $server->refresh();
$this->user = $server->getSshUser();
if ($asUser && $asUser != $server->getSshUser()) {
if ($asUser && $asUser !== $server->getSshUser()) {
$this->asUser = $asUser;
}
$this->privateKey = PublicKeyLoader::loadPrivateKey(
file_get_contents($this->server->sshKey()['private_key_path'])
(string) file_get_contents($this->server->sshKey()['private_key_path'])
);
return $this;
@ -94,9 +94,9 @@ public function connect(bool $sftp = false): void
*/
public function exec(string $command, string $log = '', ?int $siteId = null, ?bool $stream = false, ?callable $streamCallback = null): string
{
if (! $this->log && $log) {
$this->log = ServerLog::make($this->server, $log);
if ($siteId) {
if (! $this->log instanceof ServerLog && $log) {
$this->log = ServerLog::newLog($this->server, $log);
if ($siteId !== null && $siteId !== 0) {
$this->log->forSite($siteId);
}
$this->log->save();
@ -111,14 +111,15 @@ public function exec(string $command, string $log = '', ?int $siteId = null, ?bo
}
try {
if ($this->asUser) {
if ($this->asUser !== null && $this->asUser !== '' && $this->asUser !== '0') {
$command = addslashes($command);
$command = str_replace('\\\'', '\'', $command);
$command = 'sudo su - '.$this->asUser.' -c '.'"'.trim($command).'"';
}
$this->connection->setTimeout(0);
if ($stream) {
if ($stream === true) {
/** @var callable $streamCallback */
$this->connection->exec($command, function ($output) use ($streamCallback) {
$this->log?->write($output);
@ -126,22 +127,20 @@ public function exec(string $command, string $log = '', ?int $siteId = null, ?bo
});
return '';
} else {
$output = '';
$this->connection->exec($command, function ($out) use (&$output) {
$this->log?->write($out);
$output .= $out;
});
if ($this->connection->getExitStatus() !== 0 || Str::contains($output, 'VITO_SSH_ERROR')) {
throw new SSHCommandError(
message: 'SSH command failed with an error',
log: $this->log
);
}
return $output;
}
$output = '';
$this->connection->exec($command, function (string $out) use (&$output): void {
$this->log?->write($out);
$output .= $out;
});
if ($this->connection->getExitStatus() !== 0 || Str::contains($output, 'VITO_SSH_ERROR')) {
throw new SSHCommandError(
message: 'SSH command failed with an error',
log: $this->log
);
}
return $output;
} catch (Throwable $e) {
Log::error('Error executing command', [
'msg' => $e->getMessage(),
@ -168,10 +167,11 @@ public function upload(string $local, string $remote, ?string $owner = null): vo
$tmpName = Str::random(10).strtotime('now');
$tempPath = home_path($this->user).'/'.$tmpName;
/** @phpstan-ignore-next-line */
$this->connection->put($tempPath, $local, SFTP::SOURCE_LOCAL_FILE);
$this->exec(sprintf('sudo mv %s %s', $tempPath, $remote));
if (! $owner) {
if ($owner === null || $owner === '' || $owner === '0') {
$owner = $this->user;
}
$this->exec(sprintf('sudo chown %s:%s %s', $owner, $owner, $remote));
@ -189,6 +189,7 @@ public function download(string $local, string $remote): void
$this->connect(true);
}
/** @phpstan-ignore-next-line */
$this->connection->get($remote, $local);
}
@ -220,7 +221,7 @@ public function write(string $remotePath, string $content, ?string $owner = null
*/
public function disconnect(): void
{
if ($this->connection) {
if ($this->connection instanceof SSH2) {
$this->connection->disconnect();
$this->connection = null;
}

View File

@ -73,7 +73,7 @@ public function show(Project $project, Server $server, CronJob $cronJob): CronJo
#[Delete('{cronJob}', name: 'api.projects.servers.cron-jobs.delete', middleware: 'ability:write')]
#[Endpoint(title: 'delete', description: 'Delete cron job.')]
#[Response(status: 204)]
public function delete(Project $project, Server $server, CronJob $cronJob)
public function delete(Project $project, Server $server, CronJob $cronJob): \Illuminate\Http\Response
{
$this->authorize('delete', [$cronJob, $server]);

View File

@ -72,7 +72,7 @@ public function show(Project $project, Server $server, Database $database): Data
#[Delete('{database}', name: 'api.projects.servers.databases.delete', middleware: 'ability:write')]
#[Endpoint(title: 'delete', description: 'Delete database.')]
#[Response(status: 204)]
public function delete(Project $project, Server $server, Database $database)
public function delete(Project $project, Server $server, Database $database): \Illuminate\Http\Response
{
$this->authorize('delete', [$database, $server]);

View File

@ -90,7 +90,7 @@ public function link(Request $request, Project $project, Server $server, Databas
#[Delete('{databaseUser}', name: 'api.projects.servers.database-users.delete', middleware: 'ability:write')]
#[Endpoint(title: 'delete', description: 'Delete database user.')]
#[Response(status: 204)]
public function delete(Project $project, Server $server, DatabaseUser $databaseUser)
public function delete(Project $project, Server $server, DatabaseUser $databaseUser): \Illuminate\Http\Response
{
$this->authorize('delete', [$databaseUser, $server]);

View File

@ -98,7 +98,7 @@ public function show(Project $project, Server $server, FirewallRule $firewallRul
#[Delete('{firewallRule}', name: 'api.projects.servers.firewall-rules.delete', middleware: 'ability:write')]
#[Endpoint(title: 'delete', description: 'Delete firewall rule.')]
#[Response(status: 204)]
public function delete(Project $project, Server $server, FirewallRule $firewallRule)
public function delete(Project $project, Server $server, FirewallRule $firewallRule): \Illuminate\Http\Response
{
$this->authorize('delete', [$firewallRule, $server]);

View File

@ -8,7 +8,9 @@
use App\Http\Controllers\Controller;
use App\Models\GitHook;
use App\Models\ServerLog;
use App\Models\SourceControl;
use App\Notifications\SourceControlDisconnected;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Spatie\RouteAttributes\Attributes\Any;
@ -17,7 +19,7 @@
class GitHookController extends Controller
{
#[Any('api/git-hooks', name: 'api.git-hooks')]
public function __invoke(Request $request)
public function __invoke(Request $request): JsonResponse
{
if (! $request->input('secret')) {
abort(404);
@ -29,7 +31,9 @@ public function __invoke(Request $request)
->firstOrFail();
foreach ($gitHook->actions as $action) {
$webhookBranch = $gitHook->site->sourceControl->provider()->getWebhookBranch($request->array());
/** @var SourceControl $sourceControl */
$sourceControl = $gitHook->site->sourceControl;
$webhookBranch = $sourceControl->provider()->getWebhookBranch($request->array());
if ($action == 'deploy' && $gitHook->site->branch === $webhookBranch) {
try {
app(Deploy::class)->run($gitHook->site);

View File

@ -14,7 +14,7 @@ class HealthController extends Controller
#[Get('api/health', name: 'api.health')]
#[Unauthenticated]
#[Endpoint(title: 'health-check')]
public function __invoke()
public function __invoke(): \Illuminate\Http\JsonResponse
{
return response()->json([
'success' => true,

View File

@ -45,7 +45,9 @@ public function create(Request $request): ProjectResource
$this->validate($request, CreateProject::rules());
$project = app(CreateProject::class)->create(auth()->user(), $request->all());
/** @var \App\Models\User $user */
$user = auth()->user();
$project = app(CreateProject::class)->create($user, $request->all());
return new ProjectResource($project);
}
@ -82,7 +84,9 @@ public function delete(Project $project): Response
{
$this->authorize('delete', $project);
app(DeleteProject::class)->delete(auth()->user(), $project);
/** @var \App\Models\User $user */
$user = auth()->user();
app(DeleteProject::class)->delete($user, $project);
return response()->noContent();
}

View File

@ -61,7 +61,9 @@ public function create(Request $request, Project $project): ServerResource
$this->validate($request, CreateServer::rules($project, $request->input()));
$server = app(CreateServer::class)->create(auth()->user(), $project, $request->all());
/** @var \App\Models\User $user */
$user = auth()->user();
$server = app(CreateServer::class)->create($user, $project, $request->all());
return new ServerResource($server);
}
@ -81,7 +83,7 @@ public function show(Project $project, Server $server): ServerResource
#[Post('{server}/reboot', name: 'api.projects.servers.reboot', middleware: 'ability:write')]
#[Endpoint(title: 'reboot', description: 'Reboot a server.')]
#[Response(status: 204)]
public function reboot(Project $project, Server $server)
public function reboot(Project $project, Server $server): \Illuminate\Http\Response
{
$this->authorize('update', [$server, $project]);
@ -95,7 +97,7 @@ public function reboot(Project $project, Server $server)
#[Post('{server}/upgrade', name: 'api.projects.servers.upgrade', middleware: 'ability:write')]
#[Endpoint(title: 'upgrade', description: 'Upgrade server.')]
#[Response(status: 204)]
public function upgrade(Project $project, Server $server)
public function upgrade(Project $project, Server $server): \Illuminate\Http\Response
{
$this->authorize('update', [$server, $project]);
@ -109,7 +111,7 @@ public function upgrade(Project $project, Server $server)
#[Delete('{server}', name: 'api.projects.servers.delete', middleware: 'ability:write')]
#[Endpoint(title: 'delete', description: 'Delete server.')]
#[Response(status: 204)]
public function delete(Project $project, Server $server)
public function delete(Project $project, Server $server): \Illuminate\Http\Response
{
$this->authorize('delete', [$server, $project]);

View File

@ -54,7 +54,9 @@ public function create(Request $request, Project $project): ServerProviderResour
$this->validate($request, CreateServerProvider::rules($request->all()));
$serverProvider = app(CreateServerProvider::class)->create(auth()->user(), $project, $request->all());
/** @var \App\Models\User $user */
$user = auth()->user();
$serverProvider = app(CreateServerProvider::class)->create($user, $project, $request->all());
return new ServerProviderResource($serverProvider);
}
@ -62,7 +64,7 @@ public function create(Request $request, Project $project): ServerProviderResour
#[Get('{serverProvider}', name: 'api.projects.server-providers.show', middleware: 'ability:read')]
#[Endpoint(title: 'show')]
#[ResponseFromApiResource(ServerProviderResource::class, ServerProvider::class)]
public function show(Project $project, ServerProvider $serverProvider)
public function show(Project $project, ServerProvider $serverProvider): \App\Http\Resources\ServerProviderResource
{
$this->authorize('view', $serverProvider);
@ -76,7 +78,7 @@ public function show(Project $project, ServerProvider $serverProvider)
#[BodyParam(name: 'name', description: 'The name of the server provider.', required: true)]
#[BodyParam(name: 'global', description: 'Accessible in all projects', enum: [true, false])]
#[ResponseFromApiResource(ServerProviderResource::class, ServerProvider::class)]
public function update(Request $request, Project $project, ServerProvider $serverProvider)
public function update(Request $request, Project $project, ServerProvider $serverProvider): \App\Http\Resources\ServerProviderResource
{
$this->authorize('update', $serverProvider);
@ -92,7 +94,7 @@ public function update(Request $request, Project $project, ServerProvider $serve
#[Delete('{serverProvider}', name: 'api.projects.server-providers.delete', middleware: 'ability:write')]
#[Endpoint(title: 'delete')]
#[Response(status: 204)]
public function delete(Project $project, ServerProvider $serverProvider)
public function delete(Project $project, ServerProvider $serverProvider): \Illuminate\Http\Response
{
$this->authorize('delete', $serverProvider);

View File

@ -52,16 +52,21 @@ public function create(Request $request, Project $project, Server $server): SshK
$this->validateRoute($project, $server);
/** @var \App\Models\User $user */
$user = auth()->user();
$sshKey = null;
if ($request->has('key_id')) {
$this->validate($request, DeployKeyToServer::rules($request->user(), $server));
$this->validate($request, DeployKeyToServer::rules($user, $server));
$sshKey = $request->user()->sshKeys()->findOrFail($request->key_id);
/** @var ?SshKey $sshKey */
$sshKey = $user->sshKeys()->findOrFail($request->key_id);
}
if (! $sshKey) {
$this->validate($request, CreateSshKey::rules());
$sshKey = app(CreateSshKey::class)->create($request->user(), $request->all());
/** @var SshKey $sshKey */
$sshKey = app(CreateSshKey::class)->create($user, $request->all());
}
app(DeployKeyToServer::class)->deploy($server, ['key_id' => $sshKey->id]);
@ -72,7 +77,7 @@ public function create(Request $request, Project $project, Server $server): SshK
#[Delete('{sshKey}', name: 'api.projects.servers.ssh-keys.delete', middleware: 'ability:write')]
#[Endpoint(title: 'delete', description: 'Delete ssh key from server.')]
#[Response(status: 204)]
public function delete(Project $project, Server $server, SshKey $sshKey)
public function delete(Project $project, Server $server, SshKey $sshKey): \Illuminate\Http\Response
{
$this->authorize('delete', [$sshKey, $server]);

View File

@ -84,7 +84,7 @@ public function show(Project $project, Server $server, Site $site): SiteResource
#[Delete('{site}', name: 'api.projects.servers.sites.delete', middleware: 'ability:write')]
#[Endpoint(title: 'delete', description: 'Delete site.')]
#[Response(status: 204)]
public function delete(Project $project, Server $server, Site $site)
public function delete(Project $project, Server $server, Site $site): \Illuminate\Http\Response
{
$this->authorize('delete', [$site, $server]);

View File

@ -55,7 +55,7 @@ public function create(Request $request, Project $project): SourceControlResourc
$this->validate($request, ConnectSourceControl::rules($request->all()));
$sourceControl = app(ConnectSourceControl::class)->connect(auth()->user(), $project, $request->all());
$sourceControl = app(ConnectSourceControl::class)->connect($project, $request->all());
return new SourceControlResource($sourceControl);
}
@ -63,7 +63,7 @@ public function create(Request $request, Project $project): SourceControlResourc
#[Get('{sourceControl}', name: 'api.projects.source-controls.show', middleware: 'ability:read')]
#[Endpoint(title: 'show')]
#[ResponseFromApiResource(SourceControlResource::class, SourceControl::class)]
public function show(Project $project, SourceControl $sourceControl)
public function show(Project $project, SourceControl $sourceControl): \App\Http\Resources\SourceControlResource
{
$this->authorize('view', $sourceControl);
@ -81,7 +81,7 @@ public function show(Project $project, SourceControl $sourceControl)
#[BodyParam(name: 'password', description: 'The password if the provider is Bitbucket')]
#[BodyParam(name: 'global', description: 'Accessible in all projects', enum: [true, false])]
#[ResponseFromApiResource(SourceControlResource::class, SourceControl::class)]
public function update(Request $request, Project $project, SourceControl $sourceControl)
public function update(Request $request, Project $project, SourceControl $sourceControl): \App\Http\Resources\SourceControlResource
{
$this->authorize('update', $sourceControl);
@ -97,7 +97,7 @@ public function update(Request $request, Project $project, SourceControl $source
#[Delete('{sourceControl}', name: 'api.projects.source-controls.delete', middleware: 'ability:write')]
#[Endpoint(title: 'delete')]
#[Response(status: 204)]
public function delete(Project $project, SourceControl $sourceControl)
public function delete(Project $project, SourceControl $sourceControl): \Illuminate\Http\Response
{
$this->authorize('delete', $sourceControl);

View File

@ -54,7 +54,9 @@ public function create(Request $request, Project $project): StorageProviderResou
$this->validate($request, CreateStorageProvider::rules($request->all()));
$storageProvider = app(CreateStorageProvider::class)->create(auth()->user(), $project, $request->all());
/** @var \App\Models\User $user */
$user = auth()->user();
$storageProvider = app(CreateStorageProvider::class)->create($user, $project, $request->all());
return new StorageProviderResource($storageProvider);
}
@ -62,7 +64,7 @@ public function create(Request $request, Project $project): StorageProviderResou
#[Get('{storageProvider}', name: 'api.projects.storage-providers.show', middleware: 'ability:read')]
#[Endpoint(title: 'show')]
#[ResponseFromApiResource(StorageProviderResource::class, StorageProvider::class)]
public function show(Project $project, StorageProvider $storageProvider)
public function show(Project $project, StorageProvider $storageProvider): \App\Http\Resources\StorageProviderResource
{
$this->authorize('view', $storageProvider);
@ -76,7 +78,7 @@ public function show(Project $project, StorageProvider $storageProvider)
#[BodyParam(name: 'name', description: 'The name of the storage provider.', required: true)]
#[BodyParam(name: 'global', description: 'Accessible in all projects', enum: [true, false])]
#[ResponseFromApiResource(StorageProviderResource::class, StorageProvider::class)]
public function update(Request $request, Project $project, StorageProvider $storageProvider)
public function update(Request $request, Project $project, StorageProvider $storageProvider): \App\Http\Resources\StorageProviderResource
{
$this->authorize('update', $storageProvider);
@ -92,7 +94,7 @@ public function update(Request $request, Project $project, StorageProvider $stor
#[Delete('{storageProvider}', name: 'api.projects.storage-providers.delete', middleware: 'ability:write')]
#[Endpoint(title: 'delete')]
#[Response(status: 204)]
public function delete(Project $project, StorageProvider $storageProvider)
public function delete(Project $project, StorageProvider $storageProvider): \Illuminate\Http\Response
{
$this->authorize('delete', $storageProvider);

Some files were not shown because too many files have changed in this diff Show More