Skip to content

Commit

Permalink
Merge branch 'ahacart-adexos-feature/handle_unable_to_connect_to_tran…
Browse files Browse the repository at this point in the history
…sporter'
  • Loading branch information
thomas-kl1 committed Aug 11, 2022
2 parents 0dfff64 + 9734430 commit ba11cb8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Handler/RotatingFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Exception;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Framework\App\Filesystem\DirectoryList;
use Monolog\Handler\HandlerInterface;

class RotatingFileHandler implements MagentoHandlerInterface
Expand Down
72 changes: 44 additions & 28 deletions Transport/UdpTransportWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

namespace Opengento\Logger\Transport;

use Exception;
use Gelf\MessageInterface as Message;
use Gelf\Transport\TransportInterface;
use Gelf\Transport\UdpTransport;
use Gelf\Transport\UdpTransportFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Psr\Log\LoggerInterface;

use in_array;
use function in_array;

/**
* Class UdpTransportWrapper
Expand All @@ -23,11 +25,21 @@
*/
class UdpTransportWrapper implements TransportInterface
{
/**
* @var UdpTransportFactory
*/
private $transportFactory;

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var string
*/
Expand All @@ -44,34 +56,41 @@ class UdpTransportWrapper implements TransportInterface
private $chunkSize;

/**
* @var UdpTransport
*/
private $transporter;

/**
* @var UdpTransportFactory
* @var string[]
*/
private $transportFactory;
private $ignoredMessages;

/**
* @var string[]
* @var UdpTransport
*/
private $ignoredMessages;
private $transporter;

public function __construct(
UdpTransportFactory $transportFactory,
ScopeConfigInterface $scopeConfig,
LoggerInterface $logger,
string $hostPath,
string $portPath,
string $chunkSize = UdpTransport::CHUNK_SIZE_LAN,
array $ignoredMessages = []
) {
$this->transportFactory = $transportFactory;
$this->scopeConfig = $scopeConfig;
$this->logger = $logger;
$this->hostPath = $hostPath;
$this->portPath = $portPath;
$this->chunkSize = $chunkSize;
$this->transportFactory = $transportFactory;
$this->ignoredMessages = $ignoredMessages;
unset($this->transporter);
}

public function __get(string $name)
{
if ($name === 'transporter') {
return $this->{$name} = $this->createTransporter();
}

return null;
}

/**
Expand All @@ -83,26 +102,23 @@ public function __construct(
*/
public function send(Message $message): int
{
if (in_array($message->getShortMessage(), $this->ignoredMessages, true)) {
return 0;
if (!in_array($message->getShortMessage(), $this->ignoredMessages, true)) {
try {
return $this->transporter->send($message);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), $e->getTrace());
}
}

return $this->getTransporter()->send($message);
return 0;
}

/**
* @return UdpTransport
*/
private function getTransporter(): UdpTransport
private function createTransporter(): UdpTransport
{
if (null === $this->transporter) {
$this->transporter = $this->transportFactory->create([
'host' => $this->scopeConfig->getValue($this->hostPath),
'port' => $this->scopeConfig->getValue($this->portPath),
'chunkSize' => $this->chunkSize
]);
}

return $this->transporter;
return $this->transporter = $this->transportFactory->create([
'host' => $this->scopeConfig->getValue($this->hostPath),
'port' => $this->scopeConfig->getValue($this->portPath),
'chunkSize' => $this->chunkSize
]);
}
}
}

0 comments on commit ba11cb8

Please sign in to comment.