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\Shopify\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
|
|
|
private $shops; |
25
|
|
|
private $access_tokens; |
|
|
|
|
26
|
|
|
|
27
|
|
|
public $controller = null; |
28
|
|
|
|
29
|
30 |
|
public function initialize(array $config = []) { |
30
|
30 |
|
$this->shops = TableRegistry::get('Multidimensional/Shopify.Shops'); |
31
|
30 |
|
$this->access_tokens = TableRegistry::get('Multidimensional/Shopify.AccessTokens'); |
32
|
30 |
|
} |
33
|
|
|
|
34
|
30 |
|
public function startup(Event $event) { |
35
|
30 |
|
$this->setController($event->subject()); |
36
|
30 |
|
} |
37
|
|
|
|
38
|
30 |
|
public function setController($controller) { |
39
|
30 |
|
$this->controller = $controller; |
40
|
30 |
|
if (!isset($this->controller->paginate)) { |
41
|
|
|
$this->controller->paginate = []; |
42
|
|
|
} |
43
|
30 |
|
} |
44
|
|
|
|
45
|
|
|
public function shopDataToDatabase(array $data) { |
46
|
|
|
|
47
|
|
|
$shop_entity = $this->shops->newEntity(); |
|
|
|
|
48
|
|
|
|
49
|
|
|
unset($data['created_at']); |
50
|
|
|
unset($data['updated_at']); |
51
|
|
|
|
52
|
|
|
$shop_entity->set($data); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$shop_entity->set(['updated_at' => new \DateTime('now')]); |
|
|
|
|
55
|
|
|
|
56
|
|
|
if (!$shop_entity->errors() && $this->shops->save($shop_entity)) { |
|
|
|
|
57
|
|
|
return $shop_entity; |
|
|
|
|
58
|
|
|
} else { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
public function accessTokenToDatabase($access_token, $shop_id, $api_key) { |
|
|
|
|
66
|
|
|
|
67
|
|
|
$access_token_entity = $this->access_tokens->newEntity(); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$access_token_array = [ |
|
|
|
|
70
|
|
|
'shop_id' => $shop_id, |
|
|
|
|
71
|
|
|
'api_key' => $api_key, |
|
|
|
|
72
|
|
|
'token' => $access_token]; |
|
|
|
|
73
|
|
|
|
74
|
|
|
$access_token_entity->set($access_token_array); |
|
|
|
|
75
|
|
|
|
76
|
|
|
$access_token_id = $this->access_tokens |
|
|
|
|
77
|
|
|
->find() |
78
|
|
|
->where($access_token_array) |
|
|
|
|
79
|
|
|
->first(); |
80
|
|
|
|
81
|
|
|
if ($access_token_id) { |
|
|
|
|
82
|
|
|
|
83
|
|
|
$access_token_entity->set([ |
|
|
|
|
84
|
|
|
'id' => $access_token_id->id, |
|
|
|
|
85
|
|
|
'updated_at' => new \DateTime('now') |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!$access_token_entity->errors() && $this->access_tokens->save($access_token_entity)) { |
|
|
|
|
91
|
|
|
return $access_token_entity; |
|
|
|
|
92
|
|
|
} else { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getShopIdFromDomain($domain) { |
99
|
|
|
|
100
|
|
|
$shop_entity = $this->shops->findByMyshopifyDomain($domain)->first(); |
|
|
|
|
101
|
|
|
if ($shop_entity->id) { |
|
|
|
|
102
|
|
|
return (int) $shop_entity->id; |
|
|
|
|
103
|
|
|
} else { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getShopDataFromAccessToken($access_token, $api_key) { |
|
|
|
|
110
|
|
|
|
111
|
|
|
$query = $this->access_tokens->find(); |
112
|
|
|
$query = $query->contain(['Shops']); |
113
|
|
|
$query = $query->where(['api_key' => $api_key, 'token' => $access_token]); |
|
|
|
|
114
|
|
|
$query = $query->where(function($exp, $q) { |
|
|
|
|
115
|
|
|
return $exp->isNull('expired_at'); |
116
|
|
|
}); |
117
|
|
|
|
118
|
|
|
$shop_entity = $query->first()->toArray(); |
|
|
|
|
119
|
|
|
|
120
|
|
|
if (is_array($shop_entity['shop'])) { |
|
|
|
|
121
|
|
|
return $shop_entity['shop']; |
|
|
|
|
122
|
|
|
} else { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getAccessTokenFromShopDomain($shop_domain, $api_key) { |
|
|
|
|
129
|
|
|
|
130
|
|
|
$query = $this->access_tokens->find(); |
131
|
|
|
$query = $query->contain(['Shops']); |
132
|
|
|
$query = $query->where(['api_key' => $api_key, 'Shops.myshopify_domain' => $shop_domain]); |
|
|
|
|
133
|
|
|
$query = $query->where(function($exp, $q) { |
|
|
|
|
134
|
|
|
return $exp->isNull('expired_at'); |
135
|
|
|
}); |
136
|
|
|
|
137
|
|
|
$access_token_entity = $query->first(); |
|
|
|
|
138
|
|
|
|
139
|
|
|
if ($access_token_entity->token) { |
|
|
|
|
140
|
|
|
return $access_token_entity->token; |
|
|
|
|
141
|
|
|
} else { |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
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
.