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