Skip to content

Commit

Permalink
Add logger in factory
Browse files Browse the repository at this point in the history
  • Loading branch information
nuradiyana committed Aug 28, 2024
1 parent 5a27603 commit d58529e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
12 changes: 3 additions & 9 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Inisiatif\WhatsappQontakPhp;

use Psr\Log\NullLogger;
use Psr\Log\LoggerInterface;
use Webmozart\Assert\Assert;
use Http\Discovery\Psr18ClientDiscovery;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function __construct(Credential $credential, HttpClient $httpClient = nul
);

$this->credential = $credential;
$this->logger = $logger;
$this->logger = is_null($logger) ? new NullLogger() : $logger;
}

public function send(string $templateId, string $channelId, Message $message): Response
Expand All @@ -71,7 +72,7 @@ public function send(string $templateId, string $channelId, Message $message): R
/** @var array $responseBody */
$responseBody = \json_decode((string) $response->getBody(), true);

$this->logInfo(sprintf('[WhatsappQontakPhp] Response %s', $response->getStatusCode()), $responseBody);
$this->logger->info(sprintf('[WhatsappQontakPhp] Response %s', $response->getStatusCode()), $responseBody);

Assert::keyExists($responseBody, 'data');

Expand Down Expand Up @@ -110,11 +111,4 @@ private function makeRequestBody(Message $message): array
{
return MessageUtil::makeRequestBody($message);
}

private function logInfo(string $message, array $context = []): void
{
if ($this->logger) {
$this->logger->info($message, $context);
}
}
}
6 changes: 4 additions & 2 deletions src/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Inisiatif\WhatsappQontakPhp;

use Psr\Log\LoggerInterface;
use Psr\Http\Client\ClientInterface as HttpClient;

final class ClientFactory
Expand All @@ -18,12 +19,13 @@ final class ClientFactory
*/
private static $nullClient = null;

public static function makeFromArray(array $config, HttpClient $httpClient = null): ClientInterface
public static function makeFromArray(array $config, HttpClient $httpClient = null, LoggerInterface $logger = null): ClientInterface
{
if (! self::$client instanceof Client) {
self::$client = new Client(
Credential::fromArray($config),
$httpClient
$httpClient,
$logger
);
}

Expand Down

0 comments on commit d58529e

Please sign in to comment.