Skip to content

Commit 285fdab

Browse files
committed
response DTO classes
1 parent d654a5a commit 285fdab

File tree

7 files changed

+63
-19
lines changed

7 files changed

+63
-19
lines changed

app/Enum/WebserviceEndpoint.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44

55
enum WebserviceEndpoint: string
66
{
7-
case HEALTH_CHECK = "health.json";
7+
case HEALTH_CHECK = "/api/index.php/v1/updates/health";
8+
case FETCH_UPDATES = "/api/index.php/v1/updates/fetch";
9+
case PREPARE_UPDATE = "/api/index.php/v1/updates/prepare";
10+
case FINALIZE_UPDATE = "/api/index.php/v1/updates/finalize";
811
}

app/Jobs/CheckSiteHealth.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace App\Jobs;
66

77
use App\Models\Site;
8-
use App\Services\SiteConnectionService;
8+
use app\Remotesite\Connection;
99
use Carbon\Carbon;
1010
use Illuminate\Contracts\Queue\ShouldQueue;
1111
use Illuminate\Foundation\Queue\Queueable;
@@ -26,7 +26,7 @@ public function __construct(protected readonly Site $site)
2626
*/
2727
public function handle(): void
2828
{
29-
/** @var SiteConnectionService $connection */
29+
/** @var Connection $connection */
3030
$connection = $this->site->connection;
3131

3232
$response = $connection->checkHealth();

app/Jobs/UpdateSite.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace App\Jobs;
66

7+
use App\Models\Site;
8+
use app\Remotesite\Connection;
79
use Illuminate\Contracts\Queue\ShouldQueue;
810
use Illuminate\Foundation\Queue\Queueable;
911

@@ -14,16 +16,19 @@ class UpdateSite implements ShouldQueue
1416
/**
1517
* Create a new job instance.
1618
*/
17-
public function __construct()
19+
public function __construct(protected readonly Site $site, protected string $targetVersion)
1820
{
19-
//
2021
}
2122

2223
/**
2324
* Execute the job.
2425
*/
2526
public function handle(): void
2627
{
27-
//
28+
/** @var Connection $connection */
29+
$connection = $this->site->connection;
30+
31+
// Test connection and get current version
32+
$healthResult = $connection->checkHealth();
2833
}
2934
}

app/Models/Site.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace App\Models;
66

7-
use App\Services\SiteConnectionService;
7+
use App\Remotesite\Connection;
88
use Illuminate\Database\Eloquent\Model;
99

1010
class Site extends Model
@@ -38,11 +38,11 @@ protected function casts(): array
3838

3939
public function getUrlAttribute(string $value): string
4040
{
41-
return rtrim($value, "/") . "/";
41+
return rtrim($value, "/");
4242
}
4343

44-
public function getConnectionAttribute(): SiteConnectionService
44+
public function getConnectionAttribute(): Connection
4545
{
46-
return new SiteConnectionService($this->url, $this->key);
46+
return new Connection($this->url, $this->key);
4747
}
4848
}

app/Services/SiteConnectionService.php renamed to app/RemoteSite/Connection.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,33 @@
22

33
declare(strict_types=1);
44

5-
namespace App\Services;
5+
namespace App\RemoteSite;
66

77
use App\Enum\HttpMethod;
88
use App\Enum\WebserviceEndpoint;
9+
use App\RemoteSite\Responses\HealthCheck;
10+
use App\RemoteSite\Responses\HealthCheck as HealthCheckResponse;
911
use GuzzleHttp\Client;
1012
use GuzzleHttp\Exception\RequestException;
1113
use GuzzleHttp\Psr7\Request;
1214
use GuzzleHttp\Psr7\Response;
1315
use Illuminate\Support\Facades\App;
1416
use Psr\Http\Message\RequestInterface;
1517

16-
class SiteConnectionService
18+
class Connection
1719
{
1820
public function __construct(protected readonly string $baseUrl, protected readonly string $key)
1921
{
2022
}
2123

22-
public function checkHealth(): array
24+
public function checkHealth(): HealthCheck
2325
{
2426
$healthData = $this->performWebserviceRequest(
2527
HttpMethod::GET,
2628
WebserviceEndpoint::HEALTH_CHECK
2729
);
2830

29-
// Perform a sanity check
30-
if (empty($healthData['cms_version'])) {
31-
throw new \Exception("Invalid health response content");
32-
}
33-
34-
return $healthData;
31+
return HealthCheckResponse::from($healthData);
3532
}
3633

3734
public function performExtractionRequest(array $data): array
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\RemoteSite\Responses;
4+
5+
abstract class BaseResponse
6+
{
7+
public static function from(array $data): self
8+
{
9+
$reflect = new \ReflectionClass(static::class);
10+
$properties = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC);
11+
12+
$arguments = [];
13+
14+
foreach ($properties as $property) {
15+
if (!array_key_exists($property->name, $data)) {
16+
continue;
17+
}
18+
19+
$arguments[$property->name] = $data[$property->name];
20+
}
21+
22+
return $reflect->newInstanceArgs($arguments);
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\RemoteSite\Responses;
4+
5+
class HealthCheck extends BaseResponse
6+
{
7+
public function __construct(
8+
public string $php_version,
9+
public string $db_type,
10+
public string $db_version,
11+
public string $cms_version,
12+
public string $server_os
13+
) {
14+
}
15+
}

0 commit comments

Comments
 (0)