88
99namespace Opengento \Logger \Transport ;
1010
11+ use Exception ;
1112use Gelf \MessageInterface as Message ;
1213use Gelf \Transport \TransportInterface ;
1314use Gelf \Transport \UdpTransport ;
1415use Gelf \Transport \UdpTransportFactory ;
1516use Magento \Framework \App \Config \ScopeConfigInterface ;
17+ use Psr \Log \LoggerInterface ;
1618
17- use in_array ;
19+ use function in_array ;
1820
1921/**
2022 * Class UdpTransportWrapper
2325 */
2426class UdpTransportWrapper implements TransportInterface
2527{
28+ /**
29+ * @var UdpTransportFactory
30+ */
31+ private $ transportFactory ;
32+
2633 /**
2734 * @var ScopeConfigInterface
2835 */
2936 private $ scopeConfig ;
3037
38+ /**
39+ * @var LoggerInterface
40+ */
41+ private $ logger ;
42+
3143 /**
3244 * @var string
3345 */
@@ -44,34 +56,41 @@ class UdpTransportWrapper implements TransportInterface
4456 private $ chunkSize ;
4557
4658 /**
47- * @var UdpTransport
48- */
49- private $ transporter ;
50-
51- /**
52- * @var UdpTransportFactory
59+ * @var string[]
5360 */
54- private $ transportFactory ;
61+ private $ ignoredMessages ;
5562
5663 /**
57- * @var string[]
64+ * @var UdpTransport
5865 */
59- private $ ignoredMessages ;
66+ private $ transporter ;
6067
6168 public function __construct (
6269 UdpTransportFactory $ transportFactory ,
6370 ScopeConfigInterface $ scopeConfig ,
71+ LoggerInterface $ logger ,
6472 string $ hostPath ,
6573 string $ portPath ,
6674 string $ chunkSize = UdpTransport::CHUNK_SIZE_LAN ,
6775 array $ ignoredMessages = []
6876 ) {
77+ $ this ->transportFactory = $ transportFactory ;
6978 $ this ->scopeConfig = $ scopeConfig ;
79+ $ this ->logger = $ logger ;
7080 $ this ->hostPath = $ hostPath ;
7181 $ this ->portPath = $ portPath ;
7282 $ this ->chunkSize = $ chunkSize ;
73- $ this ->transportFactory = $ transportFactory ;
7483 $ this ->ignoredMessages = $ ignoredMessages ;
84+ unset($ this ->transporter );
85+ }
86+
87+ public function __get (string $ name )
88+ {
89+ if ($ name === 'transporter ' ) {
90+ return $ this ->{$ name } = $ this ->createTransporter ();
91+ }
92+
93+ return null ;
7594 }
7695
7796 /**
@@ -83,26 +102,23 @@ public function __construct(
83102 */
84103 public function send (Message $ message ): int
85104 {
86- if (in_array ($ message ->getShortMessage (), $ this ->ignoredMessages , true )) {
87- return 0 ;
105+ if (!in_array ($ message ->getShortMessage (), $ this ->ignoredMessages , true )) {
106+ try {
107+ return $ this ->transporter ->send ($ message );
108+ } catch (Exception $ e ) {
109+ $ this ->logger ->error ($ e ->getMessage (), $ e ->getTrace ());
110+ }
88111 }
89112
90- return $ this -> getTransporter ()-> send ( $ message ) ;
113+ return 0 ;
91114 }
92115
93- /**
94- * @return UdpTransport
95- */
96- private function getTransporter (): UdpTransport
116+ private function createTransporter (): UdpTransport
97117 {
98- if (null === $ this ->transporter ) {
99- $ this ->transporter = $ this ->transportFactory ->create ([
100- 'host ' => $ this ->scopeConfig ->getValue ($ this ->hostPath ),
101- 'port ' => $ this ->scopeConfig ->getValue ($ this ->portPath ),
102- 'chunkSize ' => $ this ->chunkSize
103- ]);
104- }
105-
106- return $ this ->transporter ;
118+ return $ this ->transporter = $ this ->transportFactory ->create ([
119+ 'host ' => $ this ->scopeConfig ->getValue ($ this ->hostPath ),
120+ 'port ' => $ this ->scopeConfig ->getValue ($ this ->portPath ),
121+ 'chunkSize ' => $ this ->chunkSize
122+ ]);
107123 }
108- }
124+ }
0 commit comments