|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipboxfactory/patron-hubspot/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipboxfactory/patron-hubspot |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\patron\hubspot\connections; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\helpers\ArrayHelper; |
|
13
|
|
|
use flipbox\craft\hubspot\connections\SavableIntegrationConnectionInterface; |
|
14
|
|
|
use flipbox\craft\hubspot\HubSpot as HubSpotPlugin; |
|
15
|
|
|
use flipbox\craft\integration\connections\AbstractSaveableConnection; |
|
16
|
|
|
use Flipbox\OAuth2\Client\Provider\HubSpot; |
|
17
|
|
|
use Flipbox\OAuth2\Client\Provider\HubSpotResourceOwner; |
|
18
|
|
|
use flipbox\patron\records\Provider; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Flipbox Factory <[email protected]> |
|
22
|
|
|
* @since 1.0.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
class PatronConnection extends AbstractSaveableConnection implements SavableIntegrationConnectionInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use AccessTokenAuthorizationTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
public $hubId; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
public $appId; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var HubSpotResourceOwner |
|
40
|
|
|
*/ |
|
41
|
|
|
private $resourceOwner; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function displayName(): string |
|
47
|
|
|
{ |
|
48
|
|
|
return Craft::t('patron-hubspot', 'Patron OAuth Token'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public function rules() |
|
55
|
|
|
{ |
|
56
|
|
|
return array_merge( |
|
57
|
|
|
parent::rules(), |
|
58
|
|
|
[ |
|
59
|
|
|
[ |
|
60
|
|
|
[ |
|
61
|
|
|
'hubId', |
|
62
|
|
|
'appId', |
|
63
|
|
|
'provider' |
|
64
|
|
|
], |
|
65
|
|
|
'required' |
|
66
|
|
|
], |
|
67
|
|
|
[ |
|
68
|
|
|
[ |
|
69
|
|
|
'hubId', |
|
70
|
|
|
'appId', |
|
71
|
|
|
'provider' |
|
72
|
|
|
], |
|
73
|
|
|
'safe', |
|
74
|
|
|
'on' => [ |
|
75
|
|
|
static::SCENARIO_DEFAULT |
|
76
|
|
|
] |
|
77
|
|
|
] |
|
78
|
|
|
] |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @inheritdoc |
|
84
|
|
|
* @throws \Throwable |
|
85
|
|
|
*/ |
|
86
|
|
|
public function afterSave(bool $isNew, array $changedAttributes = []) |
|
87
|
|
|
{ |
|
88
|
|
|
// Delete existing lock |
|
89
|
|
|
if (null !== ($provider = ArrayHelper::getValue($changedAttributes, 'provider'))) { |
|
90
|
|
|
$condition = [ |
|
91
|
|
|
(is_numeric($provider) ? 'id' : 'handle') => $provider, |
|
92
|
|
|
'enabled' => null |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
if (null !== ($provider = Provider::findOne($condition))) { |
|
96
|
|
|
$provider->removeLock(HubSpotPlugin::getInstance()); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->getRecord(false)->addLock(HubSpotPlugin::getInstance()); |
|
101
|
|
|
|
|
102
|
|
|
parent::afterSave($isNew, $changedAttributes); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @inheritdoc |
|
107
|
|
|
* |
|
108
|
|
|
* @throws \Twig_Error_Loader |
|
109
|
|
|
* @throws \yii\base\Exception |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getSettingsHtml(): string |
|
112
|
|
|
{ |
|
113
|
|
|
$providers = Provider::find() |
|
114
|
|
|
->class(HubSpot::class) |
|
115
|
|
|
->enabled(null); |
|
116
|
|
|
|
|
117
|
|
|
return Craft::$app->view->renderTemplate( |
|
118
|
|
|
'patron-hubspot/connections/configuration', |
|
119
|
|
|
[ |
|
120
|
|
|
'connection' => $this, |
|
121
|
|
|
'providers' => $providers->all() |
|
122
|
|
|
] |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @inheritdoc |
|
128
|
|
|
* @throws \yii\base\InvalidConfigException |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getHubId(): string |
|
131
|
|
|
{ |
|
132
|
|
|
if ($this->hubId === null) { |
|
133
|
|
|
if (null === ($hubId = $this->getFromAccessTokenValues('hubId'))) { |
|
134
|
|
|
$hubId = $this->getResourceOwner()->getHubId(); |
|
135
|
|
|
} |
|
136
|
|
|
$this->hubId = $hubId ? (string)$hubId : null; |
|
137
|
|
|
} |
|
138
|
|
|
return $this->hubId; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @inheritdoc |
|
143
|
|
|
* @throws \yii\base\InvalidConfigException |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getAppId(): string |
|
146
|
|
|
{ |
|
147
|
|
|
if ($this->appId === null) { |
|
148
|
|
|
if (null === ($appId = $this->getFromAccessTokenValues('appId'))) { |
|
149
|
|
|
$appId = $this->getResourceOwner()->getAppId(); |
|
150
|
|
|
} |
|
151
|
|
|
$this->appId = $appId ? (string)$appId : null; |
|
152
|
|
|
} |
|
153
|
|
|
return $this->appId; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param string $attribute |
|
158
|
|
|
* @return string|null |
|
159
|
|
|
* @throws \yii\base\InvalidConfigException |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function getFromAccessTokenValues(string $attribute) |
|
162
|
|
|
{ |
|
163
|
|
|
$values = $this->getAccessToken()->getValues(); |
|
164
|
|
|
$value = $values[$attribute] ?? null; |
|
165
|
|
|
return $value ? (string)$value : null; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return HubSpotResourceOwner |
|
170
|
|
|
* @throws \yii\base\InvalidConfigException |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function getResourceOwner() |
|
173
|
|
|
{ |
|
174
|
|
|
if ($this->resourceOwner === null) { |
|
175
|
|
|
$this->resourceOwner = $this->getProvider()->getResourceOwner( |
|
176
|
|
|
$this->getAccessToken() |
|
|
|
|
|
|
177
|
|
|
); |
|
178
|
|
|
} |
|
179
|
|
|
return $this->resourceOwner; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param bool $restricted |
|
184
|
|
|
* @return Provider |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function getRecord(bool $restricted = true): Provider |
|
187
|
|
|
{ |
|
188
|
|
|
// Get provider from settings |
|
189
|
|
|
if (null !== ($provider = $this->provider ?? null)) { |
|
190
|
|
|
$condition = [ |
|
191
|
|
|
(is_numeric($provider) ? 'id' : 'handle') => $provider |
|
192
|
|
|
]; |
|
193
|
|
|
|
|
194
|
|
|
if ($restricted !== true) { |
|
195
|
|
|
$condition['enabled'] = null; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
$provider = Provider::findOne($condition); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
if (!$provider instanceof Provider) { |
|
202
|
|
|
$provider = new Provider(); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
$provider->class = HubSpot::class; |
|
206
|
|
|
|
|
207
|
|
|
return $provider; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.