1 | <?php |
||
2 | /** |
||
3 | * @link https://dukt.net/twitter/ |
||
4 | * @copyright Copyright (c) Dukt |
||
5 | * @license https://github.com/dukt/twitter/blob/master/LICENSE.md |
||
6 | */ |
||
7 | |||
8 | namespace dukt\twitter\services; |
||
9 | |||
10 | use Craft; |
||
11 | use dukt\twitter\errors\InvalidAccountException; |
||
12 | use dukt\twitter\models\Account; |
||
13 | use dukt\twitter\records\Account as AccountRecord; |
||
14 | use yii\base\Component; |
||
15 | use Exception; |
||
16 | |||
17 | /** |
||
18 | * OAuth Service |
||
19 | * |
||
20 | * @author Dukt <[email protected]> |
||
21 | * @since 3.0 |
||
22 | */ |
||
23 | class Accounts extends Component |
||
24 | { |
||
25 | /** |
||
26 | * Gets an account |
||
27 | * |
||
28 | * @return Account |
||
29 | */ |
||
30 | public function getAccount() |
||
31 | { |
||
32 | $result = AccountRecord::find()->one(); |
||
33 | |||
34 | if ($result) { |
||
35 | return new Account($result->toArray([ |
||
36 | 'id', |
||
37 | 'token', |
||
38 | 'tokenSecret', |
||
39 | ])); |
||
40 | } |
||
41 | |||
42 | return new Account(); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Saves an account |
||
47 | * |
||
48 | * @param Account $account |
||
49 | * @param bool $runValidation |
||
50 | * @return bool |
||
51 | * @throws InvalidAccountException |
||
52 | * @throws \yii\db\Exception |
||
53 | */ |
||
54 | public function saveAccount(Account $account, bool $runValidation = true): bool |
||
55 | { |
||
56 | $isNewAccount = !$account->id; |
||
57 | |||
58 | if ($runValidation && !$account->validate()) { |
||
59 | Craft::info('Account not saved due to validation error.', __METHOD__); |
||
60 | return false; |
||
61 | } |
||
62 | |||
63 | if ($account->id) { |
||
64 | $accountRecord = AccountRecord::find() |
||
65 | ->where(['id' => $account->id]) |
||
66 | ->one(); |
||
67 | |||
68 | if (!$accountRecord) { |
||
69 | throw new InvalidAccountException("No account exists with the ID '{$account->id}'"); |
||
70 | } |
||
71 | } else { |
||
72 | $accountRecord = new AccountRecord(); |
||
73 | } |
||
74 | |||
75 | $accountRecord->token = $account->token; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
76 | $accountRecord->tokenSecret = $account->tokenSecret; |
||
0 ignored issues
–
show
The property
tokenSecret does not exist on dukt\twitter\records\Account . Since you implemented __set , consider adding a @property annotation.
![]() |
|||
77 | |||
78 | $transaction = Craft::$app->getDb()->beginTransaction(); |
||
79 | |||
80 | try { |
||
81 | // Is the event giving us the go-ahead? |
||
82 | $accountRecord->save(false); |
||
83 | |||
84 | // Now that we have an account ID, save it on the model |
||
85 | if ($isNewAccount) { |
||
86 | $account->id = $accountRecord->id; |
||
0 ignored issues
–
show
The property
id does not exist on dukt\twitter\records\Account . Since you implemented __get , consider adding a @property annotation.
![]() |
|||
87 | } |
||
88 | |||
89 | $transaction->commit(); |
||
90 | } catch (Exception $e) { |
||
91 | $transaction->rollBack(); |
||
92 | |||
93 | throw $e; |
||
94 | } |
||
95 | |||
96 | return true; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Deletes an account |
||
101 | * |
||
102 | * @param Account $account |
||
103 | * @return bool |
||
104 | * @throws \Throwable |
||
105 | * @throws \yii\db\StaleObjectException |
||
106 | */ |
||
107 | public function deleteAccount(Account $account): bool |
||
108 | { |
||
109 | if (!$account->id) { |
||
110 | return true; |
||
111 | } |
||
112 | |||
113 | $accountRecord = AccountRecord::findOne($account->id); |
||
114 | |||
115 | if (!$accountRecord) { |
||
0 ignored issues
–
show
|
|||
116 | return true; |
||
117 | } |
||
118 | |||
119 | $accountRecord->delete(); |
||
120 | |||
121 | return true; |
||
122 | } |
||
123 | } |