1 | <?php |
||
2 | /** |
||
3 | * @link https://dukt.net/facebook/ |
||
4 | * @copyright Copyright (c) Dukt |
||
5 | * @license https://github.com/dukt/facebook/blob/master/LICENSE.md |
||
6 | */ |
||
7 | |||
8 | namespace dukt\facebook\services; |
||
9 | |||
10 | use Craft; |
||
11 | use dukt\facebook\errors\InvalidAccountException; |
||
12 | use dukt\facebook\models\Account; |
||
13 | use dukt\facebook\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 | ])); |
||
39 | } |
||
40 | |||
41 | return new Account(); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Saves an account |
||
46 | * |
||
47 | * @param Account $account |
||
48 | * @param bool $runValidation |
||
49 | * @return bool |
||
50 | * @throws InvalidAccountException |
||
51 | * @throws \yii\db\Exception |
||
52 | */ |
||
53 | public function saveAccount(Account $account, bool $runValidation = true): bool |
||
54 | { |
||
55 | $isNewAccount = !$account->id; |
||
56 | |||
57 | if ($runValidation && !$account->validate()) { |
||
58 | Craft::info('Account not saved due to validation error.', __METHOD__); |
||
59 | return false; |
||
60 | } |
||
61 | |||
62 | if ($account->id) { |
||
63 | $accountRecord = AccountRecord::find() |
||
64 | ->where(['id' => $account->id]) |
||
65 | ->one(); |
||
66 | |||
67 | if (!$accountRecord) { |
||
68 | throw new InvalidAccountException(sprintf('No account exists with the ID \'%s\'', $account->id)); |
||
69 | } |
||
70 | } else { |
||
71 | $accountRecord = new AccountRecord(); |
||
72 | } |
||
73 | |||
74 | $accountRecord->token = $account->token; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
75 | |||
76 | $transaction = Craft::$app->getDb()->beginTransaction(); |
||
77 | |||
78 | try { |
||
79 | // Is the event giving us the go-ahead? |
||
80 | $accountRecord->save(false); |
||
81 | |||
82 | // Now that we have an account ID, save it on the model |
||
83 | if ($isNewAccount) { |
||
84 | $account->id = $accountRecord->id; |
||
0 ignored issues
–
show
The property
id does not exist on dukt\facebook\records\Account . Since you implemented __get , consider adding a @property annotation.
Loading history...
|
|||
85 | } |
||
86 | |||
87 | $transaction->commit(); |
||
88 | } catch (Exception $exception) { |
||
89 | $transaction->rollBack(); |
||
90 | |||
91 | throw $exception; |
||
92 | } |
||
93 | |||
94 | return true; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Deletes an account |
||
99 | * |
||
100 | * @param Account $account |
||
101 | * @return bool |
||
102 | * @throws \Throwable |
||
103 | * @throws \yii\db\StaleObjectException |
||
104 | */ |
||
105 | public function deleteAccount(Account $account): bool |
||
106 | { |
||
107 | if (!$account->id) { |
||
108 | return true; |
||
109 | } |
||
110 | |||
111 | $accountRecord = AccountRecord::findOne($account->id); |
||
112 | |||
113 | if (!$accountRecord instanceof \dukt\facebook\records\Account) { |
||
114 | return true; |
||
115 | } |
||
116 | |||
117 | $accountRecord->delete(); |
||
118 | |||
119 | return true; |
||
120 | } |
||
121 | } |