| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of graze/dynamark3-client. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @license https://github.com/graze/dynamark3-client/blob/master/LICENSE.md | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @link https://github.com/graze/dynamark3-client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace Graze\Dynamark3Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Graze\TelnetClient\TelnetClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Graze\Dynamark3Client\CommandResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Graze\Dynamark3Client\Command\CommandInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Graze\TelnetClient\TelnetClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Graze\Dynamark3Client\Dynamark3Constants; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | class Dynamark3Client | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * @var TelnetClientInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     protected $telnet; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @var CommandResolver | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     protected $commandResolver; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @param TelnetClientInterface $telnet | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * @param CommandResolver $commandResolver | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 2 |  |     public function __construct(TelnetClientInterface $telnet, CommandResolver $commandResolver) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 2 |  |         $this->telnet = $telnet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 2 |  |         $this->commandResolver = $commandResolver; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @param string $name | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @param array $arguments | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @return \Graze\Dynamark3Client\Dynamark3ResponseInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |     public function __call($name, array $arguments) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 2 |  |         $command = $this->commandResolver->resolve($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |         array_unshift($arguments, $command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |         return call_user_func_array([$this, 'send'], $arguments); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * @param CommandInterface $command | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * @return \Graze\Dynamark3Client\Dynamark3ResponseInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |     protected function send(CommandInterface $command) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |         $arguments =  func_get_args(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |         $command = array_shift($arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |         $argumentsString = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2 |  |         if (!empty($arguments)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             // add quotes to arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |             array_walk($arguments, function (&$value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |                 $value = sprintf('"%s"', $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |             $argumentsString = ' ' . implode(' ', $arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |         $telnetResponse = $this->telnet->execute( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |             $command->getCommandText() . $argumentsString, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  |             $command->getPrompt() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  |         return $command->parseResponse($telnetResponse); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * @param string $dsn | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * @return Dynamark3Client | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public static function build($dsn) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $telnetClient = TelnetClient::build( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $dsn, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             Dynamark3Constants::PROMPT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             Dynamark3Constants::PROMPT_ERROR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             Dynamark3Constants::LINE_ENDING | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         return new static( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $telnetClient, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             new CommandResolver() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  |