| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace flipbox\patron\cp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Craft; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use flipbox\patron\events\RegisterProviderInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use flipbox\patron\events\RegisterProviders; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use flipbox\patron\events\RegisterProviderSettings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use flipbox\patron\helpers\ProviderHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use flipbox\patron\Patron; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use flipbox\patron\settings\FacebookSettings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use League\OAuth2\Client\Provider\Facebook; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use yii\base\Module; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use yii\web\NotFoundHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @property Patron $module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class Cp extends Module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $info; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $providers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         parent::init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         RegisterProviderSettings::on( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             Facebook::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             RegisterProviderSettings::REGISTER_SETTINGS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             function (RegisterProviderSettings $event) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 $event->class = FacebookSettings::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @throws NotFoundHttpException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     public function beforeAction($action) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         if (!Craft::$app->request->getIsCpRequest()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             throw new NotFoundHttpException(); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         return parent::beforeAction($action); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /******************************************* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * PROVIDERS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      *******************************************/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function getProviders(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if ($this->providers === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $event = new RegisterProviders(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $this->trigger( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 $event::REGISTER_PROVIDERS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $event | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $this->providers = $event->providers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return $this->providers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * This should give is some additional information about the provider. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      *      Provider::class => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      *          'name' => 'Provider Name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      *          'icon' => 'path/to/icon.svg' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      *      ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @throws \ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function getProviderInfo(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if ($this->info === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $event = new RegisterProviderInfo(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $this->trigger( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 $event::REGISTER_INFO, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 $event | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $this->info = $event->info; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $this->formatInfoArray($this->info); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return $this->info; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @param array $providers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @throws \ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     private function formatInfoArray(array &$providers) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         foreach ($providers as $class => &$provider) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             if (is_string($provider)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 $provider = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                     'icon' => $provider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             $provider['name'] = $provider['name'] ?? ProviderHelper::displayName($class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 133 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |  |