| 1 |  |  | <?php | 
            
                                                        
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 3 |  |  | namespace Bankiru\Api\Doctrine; | 
            
                                                        
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 5 |  |  | use ScayTrase\Api\Rpc\RpcClientInterface; | 
            
                                                        
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 7 |  |  | final class ClientRegistry implements ClientRegistryInterface | 
            
                                                        
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                        
            
                                    
            
            
                | 9 |  |  |     /** @var  RpcClientInterface[] */ | 
            
                                                        
            
                                    
            
            
                | 10 |  |  |     private $clients = []; | 
            
                                                        
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |      * @param string $name | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |      * | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |      * @return RpcClientInterface | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |      * @throws \OutOfBoundsException | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |     public function get($name) | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |         if (!$this->has($name)) { | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |             throw new \OutOfBoundsException(sprintf('Client "%s" not registered', $name)); | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |         return $this->clients[$name]; | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |      * @param string $name | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |      * @return bool | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |     public function has($name) | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |         return array_key_exists($name, $this->clients); | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |      * @param string             $name | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |      * @param RpcClientInterface $client | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |      * @return void | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |      * @throws \InvalidArgumentException | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |     public function add($name, RpcClientInterface $client) | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |         if ($this->has($name)) { | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |             throw new \InvalidArgumentException(); | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |         $this->replace($name, $client); | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |      * @param string             $name | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |      * @param RpcClientInterface $client | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |      * @return void | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |     public function replace($name, RpcClientInterface $client) | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         $this->clients[$name] = $client; | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |      * @return RpcClientInterface[] | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |     public function all() | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |         return $this->clients; | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 71 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  |