1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/patron-salesforce/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/patron-salesforce |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\patron\salesforce\connections; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\helpers\ArrayHelper; |
13
|
|
|
use flipbox\craft\integration\connections\AbstractSaveableConnection; |
14
|
|
|
use flipbox\craft\salesforce\connections\SavableConnectionInterface; |
15
|
|
|
use flipbox\craft\salesforce\Force; |
16
|
|
|
use flipbox\patron\records\Provider; |
17
|
|
|
use Psr\Http\Message\RequestInterface; |
18
|
|
|
use Stevenmaguire\OAuth2\Client\Provider\Salesforce; |
19
|
|
|
use Zend\Diactoros\Uri; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Flipbox Factory <[email protected]> |
23
|
|
|
* @since 1.0.0 |
24
|
|
|
*/ |
25
|
|
|
class PatronConnection extends AbstractSaveableConnection implements SavableConnectionInterface |
26
|
|
|
{ |
27
|
|
|
use AccessTokenAuthorizationTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public $version; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string|int |
36
|
|
|
*/ |
37
|
|
|
public $provider; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
public static function displayName(): string |
43
|
|
|
{ |
44
|
|
|
return Craft::t('patron-salesforce', 'Patron OAuth Token'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
|
|
public function rules() |
51
|
|
|
{ |
52
|
|
|
return array_merge( |
53
|
|
|
parent::rules(), |
54
|
|
|
[ |
55
|
|
|
[ |
56
|
|
|
[ |
57
|
|
|
'version', |
58
|
|
|
'provider' |
59
|
|
|
], |
60
|
|
|
'required' |
61
|
|
|
], |
62
|
|
|
[ |
63
|
|
|
[ |
64
|
|
|
'version', |
65
|
|
|
'provider' |
66
|
|
|
], |
67
|
|
|
'safe', |
68
|
|
|
'on' => [ |
69
|
|
|
static::SCENARIO_DEFAULT |
70
|
|
|
] |
71
|
|
|
] |
72
|
|
|
] |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
* @throws \Throwable |
79
|
|
|
*/ |
80
|
|
|
public function afterSave(bool $isNew, array $changedAttributes) |
81
|
|
|
{ |
82
|
|
|
// Delete existing lock |
83
|
|
|
if (null !== ($provider = ArrayHelper::getValue($changedAttributes, 'provider'))) { |
84
|
|
|
$condition = [ |
85
|
|
|
(is_numeric($provider) ? 'id' : 'handle') => $provider, |
86
|
|
|
'enabled' => null |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
if (null !== ($provider = Provider::findOne($condition))) { |
90
|
|
|
$provider->removeLock(Force::getInstance()); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->getRecord(false)->addLock(Force::getInstance()); |
|
|
|
|
95
|
|
|
|
96
|
|
|
parent::afterSave($isNew, $changedAttributes); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritdoc |
101
|
|
|
* @throws \yii\base\InvalidConfigException |
102
|
|
|
*/ |
103
|
|
|
public function getResourceUrl(): string |
104
|
|
|
{ |
105
|
|
|
$version = $this->version; |
106
|
|
|
|
107
|
|
|
return $this->getInstanceUrl() . |
108
|
|
|
'/services/data' . |
109
|
|
|
(!empty($version) ? ('/' . $version) : ''); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritdoc |
114
|
|
|
* |
115
|
|
|
* @throws \Twig_Error_Loader |
116
|
|
|
* @throws \yii\base\Exception |
117
|
|
|
*/ |
118
|
|
|
public function getSettingsHtml(): string |
119
|
|
|
{ |
120
|
|
|
$providers = Provider::find() |
121
|
|
|
->class(Salesforce::class) |
122
|
|
|
->enabled(null); |
123
|
|
|
|
124
|
|
|
return Craft::$app->view->renderTemplate( |
125
|
|
|
'patron-salesforce/connections/configuration', |
126
|
|
|
[ |
127
|
|
|
'connection' => $this, |
128
|
|
|
'providers' => $providers->all() |
129
|
|
|
] |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param bool $restricted |
135
|
|
|
* @return Provider |
136
|
|
|
*/ |
137
|
|
|
protected function getRecord(bool $restricted = true): Provider |
138
|
|
|
{ |
139
|
|
|
// Get provider from settings |
140
|
|
|
if (null !== ($provider = $this->provider ?? null)) { |
141
|
|
|
$condition = [ |
142
|
|
|
(is_numeric($provider) ? 'id' : 'handle') => $provider |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
if ($restricted !== true) { |
146
|
|
|
$condition['enabled'] = null; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$provider = Provider::findOne($condition); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (!$provider instanceof Provider) { |
153
|
|
|
$provider = new Provider(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$provider->class = Salesforce::class; |
157
|
|
|
|
158
|
|
|
return $provider; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @inheritdoc |
163
|
|
|
* @throws \yii\base\InvalidConfigException |
164
|
|
|
*/ |
165
|
|
|
public function getInstanceUrl(): string |
166
|
|
|
{ |
167
|
|
|
return rtrim($this->getProvider()->getDomain(), '/'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param RequestInterface $request |
172
|
|
|
* @return RequestInterface |
173
|
|
|
* @throws \yii\base\InvalidConfigException |
174
|
|
|
*/ |
175
|
|
|
public function prepareInstanceRequest(RequestInterface $request): RequestInterface |
176
|
|
|
{ |
177
|
|
|
$request = $request->withUri( |
178
|
|
|
new Uri($this->getResourceUrl()) |
179
|
|
|
); |
180
|
|
|
|
181
|
|
|
foreach ($this->getProvider()->getHeaders() as $key => $value) { |
182
|
|
|
$request = $request->withAddedHeader($key, $value); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $request; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: