| 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\Component; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Cake\Controller\Component; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Cake\ORM\TableRegistry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Cake\Event\Event; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class ShopifyDatabaseComponent extends Component | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $shops; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $access_tokens; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public $controller = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function initialize(array $config = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->shops = TableRegistry::get('Multidimensional/Cakephpify.Shops'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->access_tokens = TableRegistry::get('Multidimensional/Cakephpify.AccessTokens'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function startup(Event $event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->setController($event->subject()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function setController($controller) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->controller = $controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if (!isset($this->controller->paginate)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $this->controller->paginate = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function shopDataToDatabase(array $data) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $shopEntity = $this->shops->newEntity(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         unset($data['created_at']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         unset($data['updated_at']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |           | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $shop_entity->set($data); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $shop_entity->set(['updated_at' => new \DateTime('now')]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         if (!$shop_entity->errors() && $this->shops->save($shop_entity)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             return $shop_entity->toArray(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     public function accessTokenToDatabase($accessToken, $shopId, $apiKey) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $accessTokenEntity = $this->access_tokens->newEntity(); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         $accessTokenArray = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             'shop_id' => $shopId, | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             'api_key' => $apiKey, | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             'token' => $accessToken]; | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         $accessTokenEntity->set($accessTokenArray); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         $accessTokenId = $this->access_tokens | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             ->find() | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             ->where($accessTokenArray) | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             ->first(); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         if ($accessTokenId) { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             $accessTokenEntity->set([ | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |                 'id' => $accessTokenId->id, | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |                 'updated_at' => new \DateTime('now') | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |             ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |                                      | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         if (!$access_token_entity->errors() && $this->access_tokens->save($access_token_entity)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             return $access_token_entity->toArray(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public function getShopIdFromDomain($domain) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $shopEntity = $this->shops->findByMyshopifyDomain($domain)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if ($shopEntity->id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             return (int)$shopEntity->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     public function getShopDataFromAccessToken($accessToken, $apiKey) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $query = $this->access_tokens->find(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $query = $query->contain(['Shops']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $query = $query->where(['api_key' => $apiKey, 'token' => $accessToken]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $query = $query->where(function ($exp, $q) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             return $exp->isNull('expired_at'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $shopEntity = $query->first()->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         if (is_array($shopEntity['shop'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             return $shopEntity['shop']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     public function getAccessTokenFromShopDomain($shopDomain, $apiKey) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $query = $this->access_tokens->find(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $query = $query->contain(['Shops']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $query = $query->where(['api_key' => $apiKey, 'Shops.myshopify_domain' => $shopDomain]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $query = $query->where(function ($exp, $q) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             return $exp->isNull('expired_at'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $accessTokenEntity = $query->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         if ($accessTokenEntity->token) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             return $accessTokenEntity->token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |  | 
            
                        
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.