mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-05 16:02:34 +00:00
Add phpstan level 7(#544)
This commit is contained in:
@ -53,7 +53,7 @@ public function data(array $input): array
|
||||
public function connect(?array $credentials = null): bool
|
||||
{
|
||||
try {
|
||||
$this->connectToEc2ClientTest($credentials);
|
||||
$this->connectToEc2ClientTest($credentials ?? []);
|
||||
$this->ec2Client->describeInstances();
|
||||
|
||||
return true;
|
||||
@ -120,7 +120,10 @@ public function regions(): array
|
||||
|
||||
$regions = $this->ec2Client->describeRegions();
|
||||
|
||||
return collect($regions->toArray()['Regions'] ?? [])
|
||||
/** @var array<int, array{RegionName: string}> $regionsArray */
|
||||
$regionsArray = $regions->toArray()['Regions'] ?? [];
|
||||
|
||||
return collect($regionsArray)
|
||||
->mapWithKeys(fn ($value) => [$value['RegionName'] => $value['RegionName']])
|
||||
->toArray();
|
||||
}
|
||||
@ -182,8 +185,8 @@ private function connectToEc2Client(?string $region = null): void
|
||||
{
|
||||
$credentials = $this->serverProvider->getCredentials();
|
||||
|
||||
if (! $region) {
|
||||
$region = $this->server?->provider_data['region'];
|
||||
if ($region === null || $region === '' || $region === '0') {
|
||||
$region = $this->server->provider_data['region'];
|
||||
}
|
||||
|
||||
$this->ec2Client = new Ec2Client([
|
||||
@ -196,7 +199,10 @@ private function connectToEc2Client(?string $region = null): void
|
||||
]);
|
||||
}
|
||||
|
||||
private function connectToEc2ClientTest($credentials): void
|
||||
/**
|
||||
* @param array<string, mixed> $credentials
|
||||
*/
|
||||
private function connectToEc2ClientTest(array $credentials): void
|
||||
{
|
||||
$this->ec2Client = new Ec2Client([
|
||||
'region' => 'us-east-1',
|
||||
@ -302,9 +308,7 @@ private function getImageId(string $os): string
|
||||
|
||||
if (! empty($images)) {
|
||||
// Sort images by creation date to get the latest one
|
||||
usort($images, function ($a, $b) {
|
||||
return strtotime($b['CreationDate']) - strtotime($a['CreationDate']);
|
||||
});
|
||||
usort($images, fn (array $a, array $b): int => strtotime((string) $b['CreationDate']) - strtotime((string) $a['CreationDate']));
|
||||
|
||||
return $images[0]['ImageId'];
|
||||
}
|
||||
|
@ -9,15 +9,7 @@
|
||||
|
||||
abstract class AbstractProvider implements ServerProvider
|
||||
{
|
||||
protected ?Provider $serverProvider;
|
||||
|
||||
protected ?Server $server;
|
||||
|
||||
public function __construct(?Provider $serverProvider = null, ?Server $server = null)
|
||||
{
|
||||
$this->serverProvider = $serverProvider;
|
||||
$this->server = $server;
|
||||
}
|
||||
public function __construct(protected Provider $serverProvider, protected Server $server) {}
|
||||
|
||||
public function generateKeyPair(): void
|
||||
{
|
||||
|
@ -42,7 +42,7 @@ public function data(array $input): array
|
||||
return [];
|
||||
}
|
||||
|
||||
public function connect(?array $credentials = null): bool
|
||||
public function connect(array $credentials): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -63,7 +63,7 @@ public function create(): void
|
||||
$storageDisk = Storage::disk(config('core.key_pairs_disk'));
|
||||
File::copy(
|
||||
storage_path(config('core.ssh_private_key_name')),
|
||||
$storageDisk->path($this->server->id)
|
||||
$storageDisk->path((string) $this->server->id)
|
||||
);
|
||||
File::copy(
|
||||
storage_path(config('core.ssh_public_key_name')),
|
||||
|
@ -47,7 +47,7 @@ public function data(array $input): array
|
||||
/**
|
||||
* @throws CouldNotConnectToProvider
|
||||
*/
|
||||
public function connect(?array $credentials = null): bool
|
||||
public function connect(array $credentials): bool
|
||||
{
|
||||
try {
|
||||
$connect = Http::withToken($credentials['token'])->get($this->apiUrl.'/account');
|
||||
@ -65,23 +65,24 @@ public function connect(?array $credentials = null): bool
|
||||
public function plans(?string $region): array
|
||||
{
|
||||
try {
|
||||
/** @var array<string, mixed> $plans */
|
||||
$plans = Http::withToken($this->serverProvider->credentials['token'])
|
||||
->get($this->apiUrl.'/sizes', ['per_page' => 200])
|
||||
->json();
|
||||
|
||||
return collect($plans['sizes'])->filter(function ($size) use ($region) {
|
||||
return in_array($region, $size['regions']);
|
||||
})
|
||||
->mapWithKeys(function ($value) {
|
||||
return [
|
||||
$value['slug'] => __('server_providers.plan', [
|
||||
'name' => $value['description'],
|
||||
'cpu' => $value['vcpus'],
|
||||
'memory' => $value['memory'],
|
||||
'disk' => $value['disk'],
|
||||
]),
|
||||
];
|
||||
})
|
||||
/** @var array<int, array{slug: string, description: string, vcpus: int, memory: int, disk: int, regions: array<string>}> $sizes */
|
||||
$sizes = $plans['sizes'] ?? [];
|
||||
|
||||
return collect($sizes)
|
||||
->filter(fn (array $size): bool => in_array($region, $size['regions']))
|
||||
->mapWithKeys(fn (array $value): array => [
|
||||
$value['slug'] => __('server_providers.plan', [
|
||||
'name' => $value['description'],
|
||||
'cpu' => $value['vcpus'],
|
||||
'memory' => $value['memory'],
|
||||
'disk' => $value['disk'],
|
||||
]),
|
||||
])
|
||||
->toArray();
|
||||
} catch (Exception) {
|
||||
return [];
|
||||
@ -91,13 +92,16 @@ public function plans(?string $region): array
|
||||
public function regions(): array
|
||||
{
|
||||
try {
|
||||
/** @var array{regions?: array<int, array{slug: string, name: string, available: bool}>} $regions */
|
||||
$regions = Http::withToken($this->serverProvider->credentials['token'])
|
||||
->get($this->apiUrl.'/regions', ['per_page' => 200])
|
||||
->json();
|
||||
|
||||
return collect($regions['regions'])
|
||||
$regionsList = $regions['regions'] ?? []; // Ensure it's always an array
|
||||
|
||||
return collect($regionsList)
|
||||
->where('available', true)
|
||||
->mapWithKeys(fn ($value) => [$value['slug'] => $value['name']])
|
||||
->mapWithKeys(fn (array $value): array => [$value['slug'] => $value['name']])
|
||||
->toArray();
|
||||
} catch (Exception) {
|
||||
return [];
|
||||
@ -213,15 +217,17 @@ private function getImageId(string $os, string $region): int
|
||||
])
|
||||
->json();
|
||||
|
||||
$image = collect($result['images'])
|
||||
->filter(function ($image) use ($region, $version) {
|
||||
return in_array($region, $image['regions']) && str_contains($image['name'], $version);
|
||||
})
|
||||
/** @var array<int, array{ id: int, name: string, distribution: string, status: string, regions: array<string> }> $images */
|
||||
$images = $result['images'] ?? []; // Ensure $images is an array
|
||||
|
||||
$image = collect($images)
|
||||
->filter(fn (array $image): bool => in_array($region, $image['regions']) && str_contains($image['name'], (string) $version)
|
||||
)
|
||||
->where('distribution', 'Ubuntu')
|
||||
->where('status', 'available')
|
||||
->first();
|
||||
|
||||
return $image['id'];
|
||||
return $image['id'] ?? 0; // Handle the case where first() returns null
|
||||
} catch (Exception) {
|
||||
throw new Exception('Could not find image ID');
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ public function data(array $input): array
|
||||
* @throws CouldNotConnectToProvider
|
||||
* @throws ConnectionException
|
||||
*/
|
||||
public function connect(?array $credentials = null): bool
|
||||
public function connect(array $credentials): bool
|
||||
{
|
||||
$connect = Http::withToken($credentials['token'])->get($this->apiUrl.'/servers');
|
||||
if (! $connect->ok()) {
|
||||
@ -62,25 +62,25 @@ public function connect(?array $credentials = null): bool
|
||||
public function plans(?string $region): array
|
||||
{
|
||||
try {
|
||||
/** @var array{server_types?: array<int, array{name: string, cores: int, memory: int, disk: int, prices: array<int, array{location: string}>}>} $plans */
|
||||
$plans = Http::withToken($this->serverProvider->credentials['token'])
|
||||
->get($this->apiUrl.'/server_types', ['per_page' => 50])
|
||||
->json();
|
||||
|
||||
return collect($plans['server_types'])->filter(function ($type) use ($region) {
|
||||
return collect($type['prices'])->filter(function ($price) use ($region) {
|
||||
return $price['location'] === $region;
|
||||
});
|
||||
})
|
||||
->mapWithKeys(function ($value) {
|
||||
return [
|
||||
$value['name'] => __('server_providers.plan', [
|
||||
'name' => $value['name'],
|
||||
'cpu' => $value['cores'],
|
||||
'memory' => $value['memory'],
|
||||
'disk' => $value['disk'],
|
||||
]),
|
||||
];
|
||||
})
|
||||
/** @var array<int, array{name: string, cores: int, memory: int, disk: int, prices: array<int, array{location: string}>}> $serverTypes */
|
||||
$serverTypes = $plans['server_types'] ?? [];
|
||||
|
||||
return collect($serverTypes)
|
||||
->filter(fn (array $type): bool => collect($type['prices'])->contains(fn (array $price): bool => $price['location'] === $region)
|
||||
)
|
||||
->mapWithKeys(fn (array $value): array => [
|
||||
$value['name'] => __('server_providers.plan', [
|
||||
'name' => $value['name'],
|
||||
'cpu' => $value['cores'],
|
||||
'memory' => $value['memory'],
|
||||
'disk' => $value['disk'],
|
||||
]),
|
||||
])
|
||||
->toArray();
|
||||
} catch (Exception) {
|
||||
return [];
|
||||
@ -94,8 +94,11 @@ public function regions(): array
|
||||
->get($this->apiUrl.'/locations', ['per_page' => 50])
|
||||
->json();
|
||||
|
||||
return collect($regions['locations'])
|
||||
->mapWithKeys(fn ($value) => [$value['name'] => $value['city'].' - '.$value['country']])
|
||||
/** @var array<int, array{name: string, city: string, country: string}> $locations */
|
||||
$locations = $regions['locations'];
|
||||
|
||||
return collect($locations)
|
||||
->mapWithKeys(fn (array $value): array => [$value['name'] => $value['city'].' - '.$value['country']])
|
||||
->toArray();
|
||||
} catch (Exception) {
|
||||
return [];
|
||||
|
@ -47,7 +47,7 @@ public function data(array $input): array
|
||||
/**
|
||||
* @throws CouldNotConnectToProvider
|
||||
*/
|
||||
public function connect(?array $credentials = null): bool
|
||||
public function connect(array $credentials): bool
|
||||
{
|
||||
try {
|
||||
$connect = Http::withToken($credentials['token'])->get($this->apiUrl.'/account');
|
||||
@ -65,21 +65,23 @@ public function connect(?array $credentials = null): bool
|
||||
public function plans(?string $region): array
|
||||
{
|
||||
try {
|
||||
/** @var array<string, mixed> $plans */
|
||||
$plans = Http::withToken($this->serverProvider->credentials['token'])
|
||||
->get($this->apiUrl.'/linode/types')
|
||||
->json();
|
||||
|
||||
return collect($plans['data'])
|
||||
->mapWithKeys(function ($value) {
|
||||
return [
|
||||
$value['id'] => __('server_providers.plan', [
|
||||
'name' => $value['label'],
|
||||
'cpu' => $value['vcpus'],
|
||||
'memory' => $value['memory'],
|
||||
'disk' => $value['disk'],
|
||||
]),
|
||||
];
|
||||
})
|
||||
/** @var array<int, array<string, mixed>> $plansData */
|
||||
$plansData = $plans['data'];
|
||||
|
||||
return collect($plansData)
|
||||
->mapWithKeys(fn (array $value) => [
|
||||
$value['id'] => __('server_providers.plan', [
|
||||
'name' => $value['label'],
|
||||
'cpu' => $value['vcpus'],
|
||||
'memory' => $value['memory'],
|
||||
'disk' => $value['disk'],
|
||||
]),
|
||||
])
|
||||
->toArray();
|
||||
} catch (Exception) {
|
||||
return [];
|
||||
@ -89,12 +91,16 @@ public function plans(?string $region): array
|
||||
public function regions(): array
|
||||
{
|
||||
try {
|
||||
/** @var array<string, mixed> $regions */
|
||||
$regions = Http::withToken($this->serverProvider->credentials['token'])
|
||||
->get($this->apiUrl.'/regions')
|
||||
->json();
|
||||
|
||||
return collect($regions['data'])
|
||||
->mapWithKeys(fn ($value) => [$value['id'] => $value['label']])
|
||||
/** @var array<int, array<string, mixed>> $regionsData */
|
||||
$regionsData = $regions['data'];
|
||||
|
||||
return collect($regionsData)
|
||||
->mapWithKeys(fn (array $value) => [$value['id'] => $value['label']])
|
||||
->toArray();
|
||||
} catch (Exception) {
|
||||
return [];
|
||||
|
@ -4,18 +4,43 @@
|
||||
|
||||
interface ServerProvider
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function createRules(array $input): array;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function credentialValidationRules(array $input): array;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function credentialData(array $input): array;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function data(array $input): array;
|
||||
|
||||
public function connect(?array $credentials = null): bool;
|
||||
/**
|
||||
* @param array<string, mixed> $credentials
|
||||
*/
|
||||
public function connect(array $credentials): bool;
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function plans(?string $region): array;
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function regions(): array;
|
||||
|
||||
public function generateKeyPair(): void;
|
||||
|
@ -49,7 +49,7 @@ public function data(array $input): array
|
||||
/**
|
||||
* @throws CouldNotConnectToProvider
|
||||
*/
|
||||
public function connect(?array $credentials = null): bool
|
||||
public function connect(array $credentials): bool
|
||||
{
|
||||
try {
|
||||
$connect = Http::withToken($credentials['token'])->get($this->apiUrl.'/account');
|
||||
@ -67,23 +67,23 @@ public function connect(?array $credentials = null): bool
|
||||
public function plans(?string $region): array
|
||||
{
|
||||
try {
|
||||
/** @var array<string, mixed> $plans */
|
||||
$plans = Http::withToken($this->serverProvider->credentials['token'])
|
||||
->get($this->apiUrl.'/plans', ['per_page' => 500])
|
||||
->json();
|
||||
|
||||
return collect($plans['plans'])->filter(function ($plan) use ($region) {
|
||||
return in_array($region, $plan['locations']);
|
||||
})
|
||||
->mapWithKeys(function ($value) {
|
||||
return [
|
||||
$value['id'] => __('server_providers.plan', [
|
||||
'name' => $value['type'],
|
||||
'cpu' => $value['vcpu_count'],
|
||||
'memory' => $value['ram'],
|
||||
'disk' => $value['disk'],
|
||||
]),
|
||||
];
|
||||
})
|
||||
/** @var array<string, mixed> $plans */
|
||||
$plans = $plans['plans'] ?? [];
|
||||
|
||||
return collect($plans)->filter(fn (array $plan): bool => in_array($region, $plan['locations']))
|
||||
->mapWithKeys(fn (array $value) => [
|
||||
$value['id'] => __('server_providers.plan', [
|
||||
'name' => $value['type'],
|
||||
'cpu' => $value['vcpu_count'],
|
||||
'memory' => $value['ram'],
|
||||
'disk' => $value['disk'],
|
||||
]),
|
||||
])
|
||||
->toArray();
|
||||
} catch (Exception) {
|
||||
return [];
|
||||
@ -93,11 +93,15 @@ public function plans(?string $region): array
|
||||
public function regions(): array
|
||||
{
|
||||
try {
|
||||
/** @var array<string, mixed> $regions */
|
||||
$regions = Http::withToken($this->serverProvider->credentials['token'])
|
||||
->get($this->apiUrl.'/regions', ['per_page' => 500])
|
||||
->json();
|
||||
|
||||
return collect($regions['regions'])
|
||||
/** @var array<string, mixed> $regions */
|
||||
$regions = $regions['regions'] ?? [];
|
||||
|
||||
return collect($regions)
|
||||
->mapWithKeys(fn ($value) => [$value['id'] => $value['country'].' - '.$value['city']])
|
||||
->toArray();
|
||||
} catch (Exception) {
|
||||
@ -207,14 +211,16 @@ private function getImageId(string $os): int
|
||||
$version = config('core.operating_system_versions.'.$os);
|
||||
|
||||
try {
|
||||
/** @var array<string, mixed> $result */
|
||||
$result = Http::withToken($this->serverProvider->credentials['token'])
|
||||
->get($this->apiUrl.'/os', ['per_page' => 500])
|
||||
->json();
|
||||
|
||||
$image = collect($result['os'])
|
||||
->filter(function ($os) use ($version) {
|
||||
return str_contains($os['name'], $version);
|
||||
})
|
||||
/** @var array<string, mixed> $os */
|
||||
$os = $result['os'] ?? [];
|
||||
|
||||
$image = collect($os)
|
||||
->filter(fn (array $os): bool => str_contains((string) $os['name'], (string) $version))
|
||||
->where('family', 'ubuntu')
|
||||
->where('arch', 'x64')
|
||||
->first();
|
||||
|
Reference in New Issue
Block a user