1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://dukt.net/videos/ |
4
|
|
|
* @copyright Copyright (c) Dukt |
5
|
|
|
* @license https://github.com/dukt/videos/blob/v2/LICENSE.md |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dukt\videos\services; |
9
|
|
|
|
10
|
|
|
use Craft; |
11
|
|
|
use dukt\analytics\errors\InvalidViewException; |
|
|
|
|
12
|
|
|
use dukt\videos\models\Token; |
13
|
|
|
use dukt\videos\records\Token as TokenRecord; |
14
|
|
|
use Exception; |
15
|
|
|
use yii\base\Component; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Tokens service. |
19
|
|
|
* |
20
|
|
|
* An instance of the Tokens service is globally accessible via [[Plugin::oauth `VideosPlugin::$plugin->getTokens()`]]. |
21
|
|
|
* |
22
|
|
|
* @author Dukt <[email protected]> |
23
|
|
|
* @since 2.0 |
24
|
|
|
*/ |
25
|
|
|
class Tokens extends Component |
26
|
|
|
{ |
27
|
|
|
// Public Methods |
28
|
|
|
// ========================================================================= |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get a token by its gateway handle. |
32
|
|
|
* |
33
|
|
|
* @param $gatewayHandle |
34
|
|
|
* @return Token|null |
35
|
|
|
*/ |
36
|
|
|
public function getToken($gatewayHandle): ?\dukt\videos\models\Token |
37
|
|
|
{ |
38
|
|
|
$result = TokenRecord::find() |
39
|
|
|
->where(['gateway' => $gatewayHandle]) |
40
|
|
|
->one(); |
41
|
|
|
|
42
|
|
|
if (!$result) { |
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return new Token($result->toArray([ |
47
|
|
|
'id', |
48
|
|
|
'gateway', |
49
|
|
|
'accessToken', |
50
|
|
|
])); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Saves a token. |
55
|
|
|
* |
56
|
|
|
* @param Token $token |
57
|
|
|
* @param bool $runValidation |
58
|
|
|
* @return bool |
59
|
|
|
* @throws InvalidViewException |
60
|
|
|
*/ |
61
|
|
|
public function saveToken(Token $token, bool $runValidation = true): bool |
62
|
|
|
{ |
63
|
|
|
if ($runValidation && !$token->validate()) { |
64
|
|
|
Craft::info('Token not saved due to validation error.', __METHOD__); |
65
|
|
|
|
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($token->id) { |
|
|
|
|
70
|
|
|
$tokenRecord = TokenRecord::find() |
71
|
|
|
->where(['id' => $token->id]) |
72
|
|
|
->one(); |
73
|
|
|
|
74
|
|
|
if (!$tokenRecord) { |
75
|
|
|
throw new InvalidViewException(sprintf('No token exists with the ID \'%s\'', $token->id)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$isNewToken = false; |
79
|
|
|
} else { |
80
|
|
|
$tokenRecord = new TokenRecord(); |
81
|
|
|
$isNewToken = true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$tokenRecord->gateway = $token->gateway; |
|
|
|
|
85
|
|
|
$tokenRecord->accessToken = $token->accessToken; |
|
|
|
|
86
|
|
|
|
87
|
|
|
$transaction = Craft::$app->getDb()->beginTransaction(); |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
// Is the event giving us the go-ahead? |
91
|
|
|
$tokenRecord->save(false); |
92
|
|
|
|
93
|
|
|
// Now that we have a view ID, save it on the model |
94
|
|
|
if ($isNewToken) { |
95
|
|
|
$token->id = $tokenRecord->id; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$transaction->commit(); |
99
|
|
|
} catch (Exception $exception) { |
100
|
|
|
$transaction->rollBack(); |
101
|
|
|
|
102
|
|
|
throw $exception; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Deletes a token. |
110
|
|
|
* |
111
|
|
|
* @param int $id |
112
|
|
|
* @return bool |
113
|
|
|
* @throws \Throwable |
114
|
|
|
* @throws \yii\db\StaleObjectException |
115
|
|
|
*/ |
116
|
|
|
public function deleteTokenById(int $id): bool |
117
|
|
|
{ |
118
|
|
|
$tokenRecord = TokenRecord::findOne($id); |
119
|
|
|
|
120
|
|
|
if (!$tokenRecord instanceof \dukt\videos\records\Token) { |
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$tokenRecord->delete(); |
125
|
|
|
|
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths