Skip to content

Commit 5ec62b0

Browse files
committed
Implemented Logger in HttpAdapter and SshAdapter
1 parent 9ecb0cb commit 5ec62b0

4 files changed

Lines changed: 55 additions & 2 deletions

File tree

src/Adapter/Http/HttpAdapter.php

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

55
use GuzzleHttp\Client;
66
use GuzzleHttp\Exception\GuzzleException;
7+
use Psr\Log\LoggerInterface;
8+
use Psr\Log\NullLogger;
79
use Sal\Seven\Model\Http\Authentication\HttpAuthentication;
810
use Sal\Seven\Model\Http\Authentication\HttpAuthenticationType;
911
use Sal\Seven\Model\Http\Authentication\HttpBasicAuthentication;
@@ -28,9 +30,17 @@ class HttpAdapter implements HttpAdapterInterface
2830
/** @var ?string[] */
2931
private ?array $strictResolveList = null;
3032

33+
private LoggerInterface $logger;
34+
3135
public function __construct(
3236
private readonly Client $client,
3337
) {
38+
$this->logger = new NullLogger();
39+
}
40+
41+
public function setLogger(LoggerInterface $logger): void
42+
{
43+
$this->logger = $logger;
3444
}
3545

3646
/**
@@ -100,6 +110,12 @@ public function get(string $uri): HttpResponse
100110
{
101111
$options = $this->buildOptions();
102112
$uri = empty($this->baseUri) ? $uri : "{$this->baseUri}{$uri}";
113+
114+
$this->logger->debug('GET', [
115+
'uri' => $uri,
116+
'options' => $options,
117+
]);
118+
103119
$response = $this->client->get($uri, $options);
104120

105121
return new HttpResponse(
@@ -138,6 +154,11 @@ public function post(string $uri, array $parameters = [], ?string $json = null):
138154
$options
139155
);
140156

157+
$this->logger->debug('POST', [
158+
'uri' => $uri,
159+
'options' => $options,
160+
]);
161+
141162
$response = $this->client->post($uri, $options);
142163

143164
return new HttpResponse(
@@ -153,6 +174,12 @@ public function delete(string $uri): HttpResponse
153174
{
154175
$options = $this->buildOptions();
155176
$uri = empty($this->baseUri) ? $uri : "{$this->baseUri}{$uri}";
177+
178+
$this->logger->debug('DELETE', [
179+
'uri' => $uri,
180+
'options' => $options,
181+
]);
182+
156183
$response = $this->client->delete($uri, $options);
157184

158185
return new HttpResponse(
@@ -194,6 +221,11 @@ public function put(string $uri, array $parameters = [], ?string $json = null):
194221
$options
195222
);
196223

224+
$this->logger->debug('PUT', [
225+
'uri' => $uri,
226+
'options' => $options,
227+
]);
228+
197229
$response = $this->client->put($uri, $options);
198230

199231
return new HttpResponse(

src/Adapter/Http/HttpAdapterInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Sal\Seven\Adapter\Http;
44

55
use GuzzleHttp\Exception\GuzzleException;
6+
use Psr\Log\LoggerAwareInterface;
67
use Sal\Seven\Model\Http\Authentication\HttpAuthentication;
78
use Sal\Seven\Model\Http\Header\HttpHeader;
89
use Sal\Seven\Model\Http\HttpParameter;
@@ -11,7 +12,7 @@
1112
/**
1213
* @author Luca Saladino <sal65535@protonmail.com>
1314
*/
14-
interface HttpAdapterInterface
15+
interface HttpAdapterInterface extends LoggerAwareInterface
1516
{
1617
/**
1718
* @throws GuzzleException

src/Adapter/Ssh/SshAdapter.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Sal\Seven\Adapter\Ssh;
44

5+
use Psr\Log\LoggerInterface;
6+
use Psr\Log\NullLogger;
57
use Sal\Seven\Model\CommandResult;
68
use Sal\Seven\Model\File;
79
use Symfony\Component\Process\Exception\ProcessTimedOutException;
@@ -20,8 +22,12 @@ class SshAdapter implements SshAdapterInterface
2022
private string $user = 'root';
2123
private ?int $timeout = 60;
2224

25+
private LoggerInterface $logger;
26+
2327
public function __construct()
2428
{
29+
$this->logger = new NullLogger();
30+
2531
$this->options = [
2632
'-o', 'ControlMaster=auto',
2733
'-o', 'ControlPath=/tmp/php-seven-ssh-%C',
@@ -32,6 +38,11 @@ public function __construct()
3238
];
3339
}
3440

41+
public function setLogger(LoggerInterface $logger): void
42+
{
43+
$this->logger = $logger;
44+
}
45+
3546
public function setHost(string $host): void
3647
{
3748
$this->host = $host;
@@ -84,6 +95,8 @@ public function secureCopyFileDownload(
8495
["{$this->user}@{$this->host}:$sourceFilePath", $destinationFolder]
8596
);
8697

98+
$this->logger->debug(implode(' ', $commandLine));
99+
87100
$proc = new Process($commandLine);
88101
$proc->setTimeout($timeout);
89102
$proc->run();
@@ -120,6 +133,8 @@ public function secureCopyFileUpload(
120133
[$sourceFilePath, "{$this->user}@{$this->host}:$destinationFolder/"]
121134
);
122135

136+
$this->logger->debug(implode(' ', $commandLine));
137+
123138
$proc = new Process($commandLine);
124139
$proc->setTimeout($timeout);
125140
$proc->run();
@@ -170,6 +185,8 @@ public function runCommand(
170185
[(new Process($command))->getCommandLine()]
171186
);
172187

188+
$this->logger->debug(implode(' ', $commandLine));
189+
173190
$proc = new Process($commandLine);
174191

175192
if (null !== $pipedInput) {
@@ -201,6 +218,8 @@ public function waitForSshLogin(): void
201218
["{$this->user}@$this->host"]
202219
);
203220

221+
$this->logger->debug(implode(' ', $commandLine));
222+
204223
$process = new Process($commandLine);
205224
$process->setTimeout(null);
206225
do {

src/Adapter/Ssh/SshAdapterInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace Sal\Seven\Adapter\Ssh;
44

5+
use Psr\Log\LoggerAwareInterface;
56
use Sal\Seven\Model\CommandResult;
67
use Sal\Seven\Model\File;
78
use Symfony\Component\Process\Exception\ProcessTimedOutException;
89

910
/**
1011
* @author Luca Saladino <sal65535@protonmail.com>
1112
*/
12-
interface SshAdapterInterface
13+
interface SshAdapterInterface extends LoggerAwareInterface
1314
{
1415
public function setHost(string $host): void;
1516

0 commit comments

Comments
 (0)