@@ -229,10 +229,10 @@ public function isConnected()
229229 * @param string $address Server url string.
230230 * @param float $timeout Number of seconds until the connect() system call should timeout.
231231 *
232- * @return resource
233232 * @throws \Exception Exception raised if connection fails.
233+ * @return resource
234234 */
235- private function getStream ($ address , $ timeout )
235+ private function getStream ($ address , $ timeout, $ context )
236236 {
237237 $ errno = null ;
238238 $ errstr = null ;
@@ -242,7 +242,8 @@ function () {
242242 return true ;
243243 }
244244 );
245- $ fp = stream_socket_client ($ address , $ errno , $ errstr , $ timeout , STREAM_CLIENT_CONNECT );
245+
246+ $ fp = stream_socket_client ($ address , $ errno , $ errstr , $ timeout , STREAM_CLIENT_CONNECT , $ context );
246247 restore_error_handler ();
247248
248249 if ($ fp === false ) {
@@ -315,8 +316,8 @@ public function __construct(ConnectionOptions $options = null)
315316 *
316317 * @param string $payload Message data.
317318 *
318- * @return void
319319 * @throws \Exception Raises if fails sending data.
320+ * @return void
320321 */
321322 private function send ($ payload )
322323 {
@@ -394,8 +395,8 @@ private function handlePING()
394395 *
395396 * @param string $line Message command from Nats.
396397 *
397- * @return void
398398 * @throws Exception If subscription not found.
399+ * @return void
399400 * @codeCoverageIgnore
400401 */
401402 private function handleMSG ($ line )
@@ -408,7 +409,7 @@ private function handleMSG($line)
408409 if (count ($ parts ) === 5 ) {
409410 $ length = trim ($ parts [4 ]);
410411 $ subject = $ parts [3 ];
411- } else if (count ($ parts ) === 4 ) {
412+ } elseif (count ($ parts ) === 4 ) {
412413 $ length = trim ($ parts [3 ]);
413414 $ subject = $ parts [1 ];
414415 }
@@ -443,19 +444,34 @@ public function connect($timeout = null)
443444 }
444445
445446 $ this ->timeout = $ timeout ;
446- $ this ->streamSocket = $ this ->getStream ($ this ->options ->getAddress (), $ timeout );
447+ $ this ->streamSocket = $ this ->getStream (
448+ $ this ->options ->getAddress (), $ timeout , $ this ->options ->getStreamContext ());
447449 $ this ->setStreamTimeout ($ timeout );
448450
449- $ msg = 'CONNECT ' .$ this ->options ;
450- $ this ->send ($ msg );
451- $ connectResponse = $ this ->receive ();
451+ $ infoResponse = $ this ->receive ();
452452
453- if ($ this ->isErrorResponse ($ connectResponse ) === true ) {
454- throw Exception::forFailedConnection ($ connectResponse );
453+ if ($ this ->isErrorResponse ($ infoResponse ) === true ) {
454+ throw Exception::forFailedConnection ($ infoResponse );
455455 } else {
456- $ this ->processServerInfo ($ connectResponse );
456+ $ this ->processServerInfo ($ infoResponse );
457+ if ($ this ->serverInfo ->isTLSRequired ()) {
458+ set_error_handler (
459+ function ($ errno , $ errstr , $ errfile , $ errline ) {
460+ restore_error_handler ();
461+ throw Exception::forFailedConnection ($ errstr );
462+ });
463+
464+ if (!stream_socket_enable_crypto (
465+ $ this ->streamSocket , true , STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT)) {
466+ throw Exception::forFailedConnection ('Error negotiating crypto ' );
467+ }
468+
469+ restore_error_handler ();
470+ }
457471 }
458472
473+ $ msg = 'CONNECT ' .$ this ->options ;
474+ $ this ->send ($ msg );
459475 $ this ->ping ();
460476 $ pingResponse = $ this ->receive ();
461477
@@ -560,9 +576,9 @@ public function unsubscribe($sid, $quantity = null)
560576 * @param string $payload Message data.
561577 * @param string $inbox Message inbox.
562578 *
579+ * @throws Exception If subscription not found.
563580 * @return void
564581 *
565- * @throws Exception If subscription not found.
566582 */
567583 public function publish ($ subject , $ payload = null , $ inbox = null )
568584 {
0 commit comments