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\Event\Event; |
20
|
|
|
use Cake\ORM\TableRegistry; |
21
|
|
|
|
22
|
|
|
class ShopifyDatabaseComponent extends Component |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
private $shops; |
26
|
|
|
private $access_tokens; |
|
|
|
|
27
|
|
|
|
28
|
|
|
public $controller = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $config |
|
|
|
|
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function initialize(array $config = []) |
35
|
|
|
{ |
36
|
|
|
$this->shops = TableRegistry::get('Multidimensional/Cakephpify.Shops'); |
37
|
|
|
$this->access_tokens = TableRegistry::get('Multidimensional/Cakephpify.AccessTokens'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Event $event |
|
|
|
|
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function startup(Event $event) |
45
|
|
|
{ |
46
|
|
|
$this->setController($event->subject()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param controller $controller |
|
|
|
|
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function setController($controller) |
54
|
|
|
{ |
55
|
|
|
$this->controller = $controller; |
56
|
|
|
if (!isset($this->controller->paginate)) { |
57
|
|
|
$this->controller->paginate = []; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $data |
|
|
|
|
63
|
|
|
* @return array|bool |
|
|
|
|
64
|
|
|
*/ |
65
|
|
|
public function shopDataToDatabase(array $data) |
66
|
|
|
{ |
67
|
|
|
$shopEntity = $this->shops->newEntity(); |
68
|
|
|
|
69
|
|
|
unset($data['created_at']); |
70
|
|
|
unset($data['updated_at']); |
71
|
|
|
|
72
|
|
|
$shopEntity->set($data); |
73
|
|
|
|
74
|
|
|
$shopEntity->set(['updated_at' => new \DateTime('now')]); |
75
|
|
|
|
76
|
|
|
if (!$shopEntity->errors() && $this->shops->save($shopEntity)) { |
77
|
|
|
return $shopEntity->toArray(); |
78
|
|
|
} else { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $accessToken |
|
|
|
|
85
|
|
|
* @param int $shopId |
|
|
|
|
86
|
|
|
* @param string $apiKey |
|
|
|
|
87
|
|
|
* @return array|bool |
|
|
|
|
88
|
|
|
*/ |
89
|
|
|
public function accessTokenToDatabase($accessToken, $shopId, $apiKey) |
90
|
|
|
{ |
91
|
|
|
$accessTokenEntity = $this->access_tokens->newEntity(); |
92
|
|
|
|
93
|
|
|
$accessTokenArray = [ |
94
|
|
|
'shop_id' => $shopId, |
95
|
|
|
'api_key' => $apiKey, |
96
|
|
|
'token' => $accessToken]; |
97
|
|
|
|
98
|
|
|
$accessTokenEntity->set($accessTokenArray); |
99
|
|
|
|
100
|
|
|
$accessTokenId = $this->access_tokens |
101
|
|
|
->find() |
102
|
|
|
->where($accessTokenArray) |
103
|
|
|
->first(); |
104
|
|
|
|
105
|
|
|
if ($accessTokenId) { |
106
|
|
|
$accessTokenEntity->set( |
107
|
|
|
[ |
108
|
|
|
'id' => $accessTokenId->id, |
109
|
|
|
'updated_at' => new \DateTime('now') |
110
|
|
|
] |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (!$accessTokenEntity->errors() && $this->access_tokens->save($accessTokenEntity)) { |
115
|
|
|
return $accessTokenEntity->toArray(); |
116
|
|
|
} else { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $domain |
|
|
|
|
123
|
|
|
* @return int|false |
124
|
|
|
*/ |
125
|
|
|
public function getShopIdFromDomain($domain) |
126
|
|
|
{ |
127
|
|
|
if (empty($domain)) { |
128
|
|
|
return false; |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$shopEntity = $this |
132
|
|
|
->shops |
133
|
|
|
->findByMyshopifyDomain($domain) |
134
|
|
|
->first(); |
135
|
|
|
|
136
|
|
|
if ($shopEntity->isEmpty()) { |
137
|
|
|
return false; |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ($shopEntity->id) { |
141
|
|
|
return (int)$shopEntity->id; |
142
|
|
|
} else { |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $accessToken |
|
|
|
|
149
|
|
|
* @param string $apiKey |
|
|
|
|
150
|
|
|
* @return array|false |
151
|
|
|
*/ |
152
|
|
|
public function getShopDataFromAccessToken($accessToken, $apiKey) |
153
|
|
|
{ |
154
|
|
|
if (empty($accessToken) || empty($apiKey)) { |
155
|
|
|
return false; |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$query = $this->access_tokens->find(); |
159
|
|
|
$query = $query->contain(['Shops']); |
160
|
|
|
$query = $query->where(['api_key' => $apiKey, 'token' => $accessToken]); |
161
|
|
|
$query = $query->where( |
162
|
|
|
function ($exp, $q) { |
|
|
|
|
163
|
|
|
return $exp->isNull('expired_at'); |
164
|
|
|
} |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
$shopEntity = $query->first(); |
168
|
|
|
|
169
|
|
|
if ($shopEntity->isEmpty()) { |
170
|
|
|
return false; |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$shopArray = $shopEntity->toArray(); |
174
|
|
|
|
175
|
|
|
if (is_array($shopArray['shop'])) { |
176
|
|
|
return $shopArray['shop']; |
177
|
|
|
} else { |
178
|
|
|
return false; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $shopDomain |
|
|
|
|
184
|
|
|
* @param string $apiKey |
|
|
|
|
185
|
|
|
* @return string|bool |
186
|
|
|
*/ |
187
|
|
|
public function getAccessTokenFromShopDomain($shopDomain, $apiKey) |
188
|
|
|
{ |
189
|
|
|
if (empty($shopDomain) || empty($apiKey)) { |
190
|
|
|
return false; |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$query = $this->access_tokens->find(); |
194
|
|
|
$query = $query->contain(['Shops']); |
195
|
|
|
$query = $query->where(['api_key' => $apiKey, 'Shops.myshopify_domain' => $shopDomain]); |
196
|
|
|
$query = $query->where( |
197
|
|
|
function ($exp, $q) { |
|
|
|
|
198
|
|
|
return $exp->isNull('expired_at'); |
199
|
|
|
} |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
$accessTokenEntity = $query->first(); |
203
|
|
|
|
204
|
|
|
if ($accessTokenEntity->isEmpty()) { |
205
|
|
|
return false; |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if ($accessTokenEntity->token) { |
209
|
|
|
return $accessTokenEntity->token; |
210
|
|
|
} else { |
211
|
|
|
return false; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
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
.