PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / src / Subscriptions / Repositories / SubscriptionNotesRepository.php

SubscriptionNotesRepository.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/Subscriptions/Repositories/SubscriptionNotesRepository.php

261 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Subscriptions\Repositories;
4
5 use Give\Framework\Database\DB;
6 use Give\Framework\Exceptions\Primitives\Exception;
7 use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
8 use Give\Framework\Models\ModelQueryBuilder;
9 use Give\Framework\Support\Facades\DateTime\Temporal;
10 use Give\Helpers\Hooks;
11 use Give\Log\Log;
12 use Give\Subscriptions\Models\SubscriptionNote;
13 use Give\Subscriptions\ValueObjects\SubscriptionNoteMetaKeys;
14 use Give\Subscriptions\ValueObjects\SubscriptionNoteType;
15
16 /**
17 * NOTE: This repository is still using the old comments table.
18 * In the future, we will migrate to the new comments table.
19 *
20 * @since 4.8.0
21 */
22 class SubscriptionNotesRepository
23 {
24 /**
25 * @since 4.8.0
26 *
27 * @var string[]
28 */
29 private $requiredSubscriptionProperties = [
30 'subscriptionId',
31 'content',
32 ];
33
34 /**
35 * @since 4.8.0
36 */
37 private const COMMENT_TYPE = 'give_sub_note';
38
39 /**
40 * @since 4.8.0
41 */
42 public function getById(int $noteId): ?SubscriptionNote
43 {
44 return $this->prepareQuery()
45 ->where('comments.comment_ID', $noteId)
46 ->get();
47 }
48
49 /**
50 * @since 4.18.0 Keep the original error as the previous exception when the write fails.
51 * @since 4.8.0
52 *
53 * @throws Exception|InvalidArgumentException
54 */
55 public function insert(SubscriptionNote $subscriptionNote): void
56 {
57 if (! $subscriptionNote->type) {
58 $subscriptionNote->type = SubscriptionNoteType::ADMIN();
59 }
60
61 $this->validateSubscriptionNote($subscriptionNote);
62
63 Hooks::doAction('givewp_subscription_note_creating', $subscriptionNote);
64
65 $dateCreated = Temporal::withoutMicroseconds($subscriptionNote->createdAt ?: Temporal::getCurrentDateTime());
66 $dateCreatedFormatted = Temporal::getFormattedDateTime($dateCreated);
67
68 DB::query('START TRANSACTION');
69
70 try {
71 DB::table('comments')
72 ->insert([
73 'comment_content' => $subscriptionNote->content,
74 'comment_date' => $dateCreatedFormatted,
75 'comment_date_gmt' => get_gmt_from_date($dateCreatedFormatted),
76 'comment_post_ID' => $subscriptionNote->subscriptionId,
77 'comment_type' => self::COMMENT_TYPE,
78 'user_id' => is_admin() ? get_current_user_id() : 0,
79 ]);
80
81 $commentId = DB::last_insert_id();
82
83 if ($subscriptionNote->type->isDonor()) {
84 DB::table('commentmeta')
85 ->insert([
86 'comment_ID' => $commentId,
87 'meta_key' => SubscriptionNoteMetaKeys::TYPE,
88 'meta_value' => SubscriptionNoteType::DONOR,
89 ]);
90 }
91 } catch (Exception $exception) {
92 DB::query('ROLLBACK');
93
94 Log::error('Failed creating a subscription note', compact('subscriptionNote'));
95
96 throw new Exception('Failed creating a subscription note', 0, $exception);
97 }
98
99 DB::query('COMMIT');
100
101 $subscriptionNote->id = $commentId;
102 $subscriptionNote->createdAt = $dateCreated;
103
104 Hooks::doAction('givewp_subscription_note_created', $subscriptionNote);
105 }
106
107 /**
108 * @since 4.18.0 Keep the original error as the previous exception when the write fails.
109 * @since 4.8.0
110 *
111 * @throws Exception|InvalidArgumentException
112 */
113 public function update(SubscriptionNote $subscriptionNote): void
114 {
115 $this->validateSubscriptionNote($subscriptionNote);
116
117 Hooks::doAction('givewp_subscription_note_updating', $subscriptionNote);
118
119 DB::query('START TRANSACTION');
120
121 try {
122 DB::table('comments')
123 ->where('comment_ID', $subscriptionNote->id)
124 ->update([
125 'comment_content' => $subscriptionNote->content,
126 'comment_post_ID' => $subscriptionNote->subscriptionId,
127 'comment_type' => self::COMMENT_TYPE,
128 'user_id' => is_admin() ? get_current_user_id() : 0,
129 ]);
130
131 if ($subscriptionNote->isDirty('type') && $subscriptionNote->type->isDonor()) {
132 $this->upsertSubscriptionNoteType($subscriptionNote);
133 }
134 } catch (Exception $exception) {
135 DB::query('ROLLBACK');
136
137 Log::error('Failed updating a subscription note', compact('subscriptionNote'));
138
139 throw new Exception('Failed updating a subscription note', 0, $exception);
140 }
141
142 DB::query('COMMIT');
143
144 Hooks::doAction('givewp_subscription_note_updated', $subscriptionNote);
145 }
146
147 /**
148 * @since 4.18.0 Keep the original error as the previous exception when the write fails.
149 * @since 4.8.0
150 *
151 * @throws Exception
152 */
153 public function delete(SubscriptionNote $subscriptionNote): bool
154 {
155 DB::query('START TRANSACTION');
156
157 Hooks::doAction('givewp_subscription_note_deleting', $subscriptionNote);
158
159 try {
160 DB::table('comments')
161 ->where('comment_ID', $subscriptionNote->id)
162 ->delete();
163
164 DB::table('commentmeta')
165 ->where('comment_ID', $subscriptionNote->id)
166 ->delete();
167 } catch (Exception $exception) {
168 DB::query('ROLLBACK');
169
170 Log::error('Failed deleting a subscription note', compact('subscriptionNote'));
171
172 throw new Exception('Failed deleting a subscription note', 0, $exception);
173 }
174
175 DB::query('COMMIT');
176
177 Hooks::doAction('givewp_subscription_note_deleted', $subscriptionNote);
178
179 return true;
180 }
181
182 /**
183 * @since 4.8.0
184 */
185 public function queryBySubscriptionId(int $subscriptionId): ModelQueryBuilder
186 {
187 return $this->prepareQuery()
188 ->where('comments.comment_post_ID', $subscriptionId)
189 ->orderBy('comments.comment_ID', 'DESC');
190 }
191
192 /**
193 * @since 4.8.0
194 */
195 private function validateSubscriptionNote(SubscriptionNote $subscriptionNote): void
196 {
197 foreach ($this->requiredSubscriptionProperties as $key) {
198 if (! isset($subscriptionNote->$key)) {
199 throw new InvalidArgumentException("'$key' is required.");
200 }
201 }
202
203 if (! $subscriptionNote->subscription) {
204 throw new InvalidArgumentException('Invalid subscriptionId, Subscription does not exist');
205 }
206 }
207
208 /**
209 * @since 4.8.0
210 *
211 * @return ModelQueryBuilder<SubscriptionNote>
212 */
213 public function prepareQuery(): ModelQueryBuilder
214 {
215 $builder = new ModelQueryBuilder(SubscriptionNote::class);
216
217 return $builder->from('comments', 'comments')
218 ->select(
219 ['comments.comment_ID', 'id'],
220 ['comments.comment_post_ID', 'subscriptionId'],
221 ['comments.comment_content', 'content'],
222 ['comments.comment_date', 'createdAt']
223 )
224 ->attachMeta(
225 'commentmeta',
226 'comments.comment_ID',
227 'comment_ID',
228 ...SubscriptionNoteMetaKeys::getColumnsForAttachMetaQuery()
229 )
230 ->where('comments.comment_type', self::COMMENT_TYPE);
231 }
232
233 /**
234 * @since 4.8.0
235 */
236 private function upsertSubscriptionNoteType(SubscriptionNote $subscriptionNote): void
237 {
238 $table = DB::table('commentmeta');
239
240 $query = $table
241 ->where('comment_ID', $subscriptionNote->id)
242 ->where('meta_key', SubscriptionNoteMetaKeys::TYPE)
243 ->get();
244
245 if (! $query) {
246 $table->insert([
247 'comment_ID' => $subscriptionNote->id,
248 'meta_key' => SubscriptionNoteMetaKeys::TYPE,
249 'meta_value' => $subscriptionNote->type->getValue(),
250 ]);
251 } else {
252 $table
253 ->where('comment_ID', $subscriptionNote->id)
254 ->where('meta_key', SubscriptionNoteMetaKeys::TYPE)
255 ->update([
256 'meta_value' => $subscriptionNote->type->getValue(),
257 ]);
258 }
259 }
260 }
261