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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/Models/DeprecationInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace LKDev\HetznerCloud\Models;

class DeprecationInfo extends Model
{
/**
* @var string
*/
public $announced;

/**
* @var string
*/
public $unavailableAfter;

public function __construct(string $announced, string $unavailableAfter)
{
$this->announced = $announced;
$this->unavailableAfter = $unavailableAfter;
parent::__construct();
}

/**
* @param $input
* @return self|null
*/
public static function parse($input): ?self
{
if ($input === null) {
return null;
}

return new self($input->announced, $input->unavailable_after);
}
}
13 changes: 13 additions & 0 deletions src/Models/Servers/Types/ServerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LKDev\HetznerCloud\Models\Servers\Types;

use LKDev\HetznerCloud\Models\DeprecationInfo;
use LKDev\HetznerCloud\Models\Model;
use LKDev\HetznerCloud\Models\Prices\Prices;
use LKDev\HetznerCloud\Models\Prices\ServerTypePrice;
Expand Down Expand Up @@ -63,6 +64,16 @@ class ServerType extends Model
*/
public $architecture;

/**
* @var bool
*/
public $deprecated;

/**
* @var DeprecationInfo|null
*/
public $deprecation;

/**
* ServerType constructor.
*
Expand Down Expand Up @@ -91,6 +102,8 @@ public function setAdditionalData($input)
$this->storageType = $input->storage_type ?? null;
$this->cpuType = $input->cpu_type ?? null;
$this->architecture = property_exists($input, 'architecture') ? $input->architecture : null;
$this->deprecated = $input->deprecated ?? false;
$this->deprecation = property_exists($input, 'deprecation') ? DeprecationInfo::parse($input->deprecation) : null;

return $this;
}
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Models/ServerTypes/fixtures/server_type.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"memory": 1,
"disk": 24,
"deprecated": false,
"deprecation": null,
"prices": [
{
"location": "fsn1",
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Models/ServerTypes/fixtures/server_types.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"memory": 1,
"disk": 24,
"deprecated": false,
"deprecation": null,
"prices": [
{
"location": "fsn1",
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/Models/Servers/Types/ServerTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LKDev\Tests\Unit\Models\Servers\Types;

use LKDev\HetznerCloud\Models\DeprecationInfo;
use LKDev\HetznerCloud\Models\Prices\ServerTypePrice;
use LKDev\HetznerCloud\Models\Servers\Types\ServerType;
use LKDev\Tests\TestCase;
Expand All @@ -22,11 +23,40 @@ public function testParse()
$this->assertEquals('local', $serverType->storageType);
$this->assertEquals('shared', $serverType->cpuType);
$this->assertNull($serverType->architecture);
$this->assertFalse($serverType->deprecated);
$this->assertNull($serverType->deprecation);

$inputWithPrice = $tmp->server->server_type;
$inputWithPrice->price = $inputWithPrice->prices[0];
$serverTypeWithPrice = ServerType::parse($inputWithPrice);
$this->assertInstanceOf(ServerTypePrice::class, $serverTypeWithPrice->price);
$this->assertEquals('fsn1', $serverTypeWithPrice->price->location);
}

public function testParseWithDeprecation()
{
$input = json_decode('{
"id": 2,
"name": "cx11-ceph",
"description": "CX11 (Ceph)",
"cores": 1,
"memory": 1,
"disk": 25,
"deprecated": true,
"deprecation": {
"announced": "2023-06-01T00:00:00Z",
"unavailable_after": "2023-09-01T00:00:00Z"
},
"prices": [],
"storage_type": "network",
"cpu_type": "shared",
"architecture": "x86"
}');

$serverType = ServerType::parse($input);
$this->assertTrue($serverType->deprecated);
$this->assertInstanceOf(DeprecationInfo::class, $serverType->deprecation);
$this->assertEquals('2023-06-01T00:00:00Z', $serverType->deprecation->announced);
$this->assertEquals('2023-09-01T00:00:00Z', $serverType->deprecation->unavailableAfter);
}
}
Loading