| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * ClientCommand.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright      More in license.md | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license        https://www.ipublikuj.eu | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @author         Adam Kadlec <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @package        iPublikuj:WebSocketsWAMPClient! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @subpackage     Commands | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @since          1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @date           11.05.18 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | namespace IPub\WebSocketsWAMPClient\Commands; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Console; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Console\Input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\Console\Style; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\Console\Output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Psr\Log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use IPub\WebSocketsWAMPClient\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use IPub\WebSocketsWAMPClient\Exceptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use IPub\WebSocketsWAMPClient\Logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * MQTT client command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @package        iPublikuj:WebSocketsWAMPClient! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @subpackage     Commands | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * @author         Adam Kadlec <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 37 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 1 |  | class ClientCommand extends Console\Command\Command | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 	 * @var Client\IClient | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 	private $client; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 	 * @var Log\LoggerInterface|Log\NullLogger|NULL | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 	private $logger; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | 	 * @param Client\IClient $client | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 	 * @param Log\LoggerInterface|NULL $logger | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 	 * @param string|NULL $name | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		Client\IClient $client, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		Log\LoggerInterface $logger = NULL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		string $name = NULL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		parent::__construct($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		$this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$this->logger = $logger === NULL ? new Log\NullLogger : $logger; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 	 * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			->setName('ipub:wampclient:start') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			->setDescription('Start web sockets WAMP client.'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 	 * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	protected function execute(Input\InputInterface $input, Output\OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		$io = new Style\SymfonyStyle($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		$io->text([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			'', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			'+-------------+', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			'| WAMP client |', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			'+-------------+', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 			'', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		if ($this->logger instanceof Logger\Console) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			$this->logger->setFormatter(new Logger\Formatter\Symfony($io)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 			$this->client->connect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		} catch (Exceptions\ConnectionException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			$this->client->getLoop()->stop(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		$this->client->getLoop()->run(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 104 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  |