| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Yii\AuthClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Collection is a storage for all auth clients in the application. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Example application configuration: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * 'authClients' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *     'google' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  *         '__class' => Yiisoft\Yii\AuthClient\Clients\Google::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  *         'setClientId()' => ['google_client_id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  *         'setClientSecret()' => ['google_client_secret'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  *      ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  *     'facebook' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  *         '__class' => Yiisoft\Yii\AuthClient\Clients\Facebook::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  *         'setClientId()' => ['facebook_client_id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  *         'setClientSecret()' => ['facebook_client_secret'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  *     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  *     ... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | final class Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var AuthClientInterface[]|array list of Auth clients with their configuration in format: 'clientName' => [...] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private array $clients; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function __construct(array $clients) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 3 |  |         $this->clients = $clients; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 3 |  |      * @return AuthClientInterface[] list of auth clients. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function getClients(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $clients = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |         foreach ($this->clients as $name => $client) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $clients[$name] = $this->getClient($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |         return $clients; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param array $clients list of auth clients indexed by their names | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function setClients(array $clients): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->clients = $clients; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |      * @param string $name client name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @throws InvalidArgumentException on non existing client request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return AuthClientInterface auth client instance. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     public function getClient(string $name): AuthClientInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         if (!$this->hasClient($name)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             throw new InvalidArgumentException("Unknown auth client '{$name}'."); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 2 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 2 |  |         $client = $this->clients[$name]; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         if (!($client instanceof AuthClientInterface)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             throw new RuntimeException( | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                 'Client should be ClientInterface instance. "' . get_class($client) . '" given.' | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 2 |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 2 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         return $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * Checks if client exists in the hub. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param string $name client id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return bool whether client exist. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function hasClient(string $name): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return array_key_exists($name, $this->clients); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  |