Skip to content

Commit fedc396

Browse files
committed
refactor service to separate class
1 parent acb0ba1 commit fedc396

File tree

3 files changed

+95
-79
lines changed

3 files changed

+95
-79
lines changed

app/Jobs/CheckSiteHealth.php

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

55
use App\Enum\HttpMethod;
66
use App\Models\Site;
7+
use App\Services\SiteConnectionService;
78
use GuzzleHttp\Exception\RequestException;
89
use Illuminate\Contracts\Queue\ShouldQueue;
910
use Illuminate\Foundation\Queue\Queueable;
@@ -24,7 +25,10 @@ public function __construct(protected readonly Site $site)
2425
*/
2526
public function handle(): void
2627
{
27-
$response = $this->site->performWebserviceRequest(
28+
/** @var SiteConnectionService $connection */
29+
$connection = $this->site->connection;
30+
31+
$response = $connection->performWebserviceRequest(
2832
HttpMethod::GET,
2933
'health.json'
3034
);

app/Models/Site.php

Lines changed: 5 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,10 @@
22

33
namespace App\Models;
44

5-
use App\Enum\HttpMethod;
6-
use App\Exceptions\RemotesiteCommunicationException;
7-
use GuzzleHttp\Client;
8-
use GuzzleHttp\Exception\RequestException;
9-
use GuzzleHttp\Psr7\Request;
10-
use GuzzleHttp\Psr7\Response;
11-
use Illuminate\Database\Eloquent\Factories\HasFactory;
12-
use Illuminate\Foundation\Auth\User as Authenticatable;
13-
use Illuminate\Notifications\Notifiable;
14-
use Illuminate\Support\Facades\App;
15-
use Psr\Http\Message\RequestInterface;
16-
use Psr\Http\Message\ResponseInterface;
5+
use App\Services\SiteConnectionService;
6+
use Illuminate\Database\Eloquent\Model;
177

18-
class Site extends Authenticatable
8+
class Site extends Model
199
{
2010
protected $fillable = [
2111
'url',
@@ -49,71 +39,8 @@ public function getUrlAttribute(string $value): string
4939
return rtrim($value, "/") . "/";
5040
}
5141

52-
53-
protected function performRequest(
54-
RequestInterface $request,
55-
array $options = []
56-
): array {
57-
/** @var Client $httpClient */
58-
$httpClient = App::make(Client::class);
59-
60-
/** @var Response $response */
61-
$response = $httpClient->send(
62-
$request,
63-
$options
64-
);
65-
66-
// Validate response
67-
if (!json_validate((string) $response->getBody())) {
68-
throw new RequestException(
69-
"Invalid JSON body",
70-
$request,
71-
$response
72-
);
73-
}
74-
75-
// Return decoded body
76-
return json_decode(
77-
(string) $response->getBody(),
78-
true,
79-
512,
80-
JSON_THROW_ON_ERROR
81-
);
82-
}
83-
84-
public function performExtractionRequest(array $data, string $password): array
42+
public function getConnectionAttribute(): SiteConnectionService
8543
{
86-
$request = new Request(
87-
'POST',
88-
$this->url . 'extract.php'
89-
);
90-
91-
$data['password'] = $password;
92-
93-
return $this->performRequest(
94-
$request,
95-
[
96-
'form_params' => $data,
97-
'timeout' => 300.0
98-
]
99-
);
100-
}
101-
102-
public function performWebserviceRequest(HttpMethod $method, string $endpoint, array $data = []): array
103-
{
104-
$request = new Request(
105-
$method->name,
106-
$this->url . $endpoint,
107-
[
108-
'Authorization' => 'JUpdate-Token ' . $this->key
109-
]
110-
);
111-
112-
return $this->performRequest(
113-
$request,
114-
[
115-
"json" => $data
116-
]
117-
);
44+
return new SiteConnectionService($this->url, $this->key);
11845
}
11946
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace App\Services;
4+
5+
use App\Enum\HttpMethod;
6+
use GuzzleHttp\Client;
7+
use GuzzleHttp\Exception\RequestException;
8+
use GuzzleHttp\Psr7\Request;
9+
use GuzzleHttp\Psr7\Response;
10+
use Illuminate\Support\Facades\App;
11+
use Psr\Http\Message\RequestInterface;
12+
13+
class SiteConnectionService
14+
{
15+
public function __construct(protected readonly string $baseUrl, protected readonly string $key)
16+
{
17+
}
18+
19+
protected function performRequest(
20+
RequestInterface $request,
21+
array $options = []
22+
): array {
23+
/** @var Client $httpClient */
24+
$httpClient = App::make(Client::class);
25+
26+
/** @var Response $response */
27+
$response = $httpClient->send(
28+
$request,
29+
$options
30+
);
31+
32+
// Validate response
33+
if (!json_validate((string) $response->getBody())) {
34+
throw new RequestException(
35+
"Invalid JSON body",
36+
$request,
37+
$response
38+
);
39+
}
40+
41+
// Return decoded body
42+
return json_decode(
43+
(string) $response->getBody(),
44+
true,
45+
512,
46+
JSON_THROW_ON_ERROR
47+
);
48+
}
49+
50+
public function performExtractionRequest(array $data): array
51+
{
52+
$request = new Request(
53+
'POST',
54+
$this->baseUrl . 'extract.php'
55+
);
56+
57+
$data['password'] = $this->key;
58+
59+
return $this->performRequest(
60+
$request,
61+
[
62+
'form_params' => $data,
63+
'timeout' => 300.0
64+
]
65+
);
66+
}
67+
68+
public function performWebserviceRequest(HttpMethod $method, string $endpoint, array $data = []): array
69+
{
70+
$request = new Request(
71+
$method->name,
72+
$this->baseUrl . $endpoint,
73+
[
74+
'Authorization' => 'JUpdate-Token ' . $this->key
75+
]
76+
);
77+
78+
return $this->performRequest(
79+
$request,
80+
[
81+
"json" => $data
82+
]
83+
);
84+
}
85+
}

0 commit comments

Comments
 (0)