| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * CakePHPify : CakePHP Plugin for Shopify API Authentication | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Copyright (c) Multidimension.al (http://multidimension.al) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Github : https://github.com/multidimension-al/cakephpify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Licensed under The MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For full copyright and license information, please see the LICENSE file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Redistributions of files must retain the above copyright notice. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright     (c) Multidimension.al (http://multidimension.al) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @link          https://github.com/multidimension-al/cakephpify CakePHPify Github | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @license       http://www.opensource.org/licenses/mit-license.php MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | namespace Multidimensional\Cakephpify\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Cake\Event\Event; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Cake\Routing\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Cake\ORM\TableRegistry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Cake\Network\Session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Multidimensional\Cakephpify\Controller\AppController; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | class InstallController extends AppController | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     private $error; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function initialize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         parent::initialize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->loadComponent('Multidimensional/Cakephpify.ShopifyDatabase'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->loadComponent('Multidimensional/Cakephpify.ShopifyAPI', ['api_key' => $this->request->api_key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->loadComponent('Flash'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->error = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function add() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $is_authorized = $this->ShopifyAPI->validateHMAC($this->request->query); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if ($is_authorized) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $access_token = $this->ShopifyAPI->getAccessToken( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 $this->request->query['shop'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 $this->request->query['code'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             if ($accessToken) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 $shop = $this->ShopifyAPI->getShopData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 if (isset($shop['id'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     $shopEntity = $this->ShopifyDatabase->shopDataToDatabase($shop); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                     if ($shopEntity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                         $accessTokenEntity = $this->ShopifyDatabase->accessTokenToDatabase( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                             $accessToken, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                             $shopEntity->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                             $this->ShopifyAPI->api_key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                         if ($accessTokenEntity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                             $this->request->session()->write([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                                 'shopify_access_token_' . $this->ShopifyAPI->api_key => $accessToken, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                                 'shopify_shop_domain_' . $this->ShopifyAPI->api_key => $this->ShopifyAPI->getShopDomain() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 							$this->Auth->setUser($shop_entity); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                             return $this->redirect([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                                 'controller' => 'Shopify', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                                 'plugin' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 								'api_key' => $this->ShopifyAPI->api_key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                                  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                             $this->Flash->set("Error saving access token. Please try again."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                         $this->Flash->set("Error inserting Shopify shop data. Please try again."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                     $this->Flash->set("Error accessing Shopify API. Please try again later."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 $this->Flash->set("Invalid access token. Pleasy try again."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $this->Flash->set("Invalid authoization code. Please try again."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $this->error = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $this->render('index'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function index() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         if (!empty($this->request->query['code']) && !$this->error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $this->render('add'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |           } elseif (!empty($this->request->data['shop_domain']) && !$this->error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $valid_domain = $this->ShopifyAPI->validDomain( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 $this->request->data['shop_domain'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             if ($validDomain) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 $this->request->session()->write([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                     'shopify_shop_domain_' . $this->ShopifyAPI->api_key => $this->request->data['shop_domain'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $redirectUrl = Router::url([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                     'controller' => 'Install', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                     'action' => 'add', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     'plugin' => 'Multidimensional/Cakephpify', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                     'api_key' => $this->ShopifyAPI->api_key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 ], true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 $authUrl = $this->ShopifyAPI->getAuthorizeUrl( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                     $this->request->data['shop_domain'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                     $redirectUrl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 $this->redirect($authUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 $this->Flash->set("Invalid Shopify Domain"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } elseif (!empty($this->error)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             $this->Flash->set($this->error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     public function redirect($url, $status = 302) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $this->set('shopify_auth_url', $url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $this->render('redirect'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 144 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |  | 
            
                        
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.