CampaignPageRepository.php
7 months ago
CampaignRepository.php
1 year ago
CampaignsDataRepository.php
9 months ago
CampaignPageRepository.php
233 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Campaigns\Repositories; |
| 4 | |
| 5 | use Give\Campaigns\Actions\CreateDefaultLayoutForCampaignPage; |
| 6 | use Give\Campaigns\Models\CampaignPage; |
| 7 | use Give\Campaigns\ValueObjects\CampaignPageMetaKeys; |
| 8 | use Give\Campaigns\ValueObjects\CampaignPageStatus; |
| 9 | use Give\Framework\Database\DB; |
| 10 | use Give\Framework\Exceptions\Primitives\Exception; |
| 11 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 12 | use Give\Framework\Models\ModelQueryBuilder; |
| 13 | use Give\Framework\Support\Facades\DateTime\Temporal; |
| 14 | use Give\Helpers\Hooks; |
| 15 | use Give\Log\Log; |
| 16 | |
| 17 | /** |
| 18 | * @since 4.0.0 |
| 19 | */ |
| 20 | class CampaignPageRepository |
| 21 | { |
| 22 | /** |
| 23 | * @since 4.0.0 |
| 24 | */ |
| 25 | protected $requiredProperties = [ |
| 26 | 'campaignId', |
| 27 | ]; |
| 28 | |
| 29 | /** |
| 30 | * @since 4.0.0 |
| 31 | */ |
| 32 | public function getById(int $id): ?CampaignPage |
| 33 | { |
| 34 | return $this->prepareQuery() |
| 35 | ->where('id', $id) |
| 36 | ->get(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @since 4.0.0 |
| 41 | */ |
| 42 | public function queryByCampaignId(int $campaignId): ModelQueryBuilder |
| 43 | { |
| 44 | return $this->prepareQuery() |
| 45 | ->where('post_status', CampaignPageStatus::TRASH()->getValue(), '!=') |
| 46 | ->where('postmeta_attach_meta_campaignId.meta_value', $campaignId); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @since 4.0.0 |
| 51 | */ |
| 52 | public function findByCampaignId(int $campaignId): ?CampaignPage |
| 53 | { |
| 54 | return $this->queryByCampaignId($campaignId)->get(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @since 4.0.0 |
| 59 | * @throws Exception |
| 60 | */ |
| 61 | public function insert(CampaignPage $campaignPage): void |
| 62 | { |
| 63 | $this->validate($campaignPage); |
| 64 | |
| 65 | Hooks::doAction('givewp_campaign_page_creating', $campaignPage); |
| 66 | |
| 67 | $dateCreated = Temporal::withoutMicroseconds($campaignPage->createdAt ?: Temporal::getCurrentDateTime()); |
| 68 | $dateCreatedFormatted = Temporal::getFormattedDateTime($dateCreated); |
| 69 | $dateUpdated = $campaignPage->updatedAt ?? $dateCreated; |
| 70 | $dateUpdatedFormatted = Temporal::getFormattedDateTime($dateUpdated); |
| 71 | $status = $campaignPage->status ?? CampaignPageStatus::DRAFT(); |
| 72 | $campaign = $campaignPage->campaign(); |
| 73 | |
| 74 | if (!$campaign) { |
| 75 | throw new Exception('Campaign not found'); |
| 76 | } |
| 77 | |
| 78 | DB::query('START TRANSACTION'); |
| 79 | |
| 80 | try { |
| 81 | $campaignPage->id = wp_insert_post([ |
| 82 | 'post_title' => $campaign->title, |
| 83 | 'post_name' => sanitize_title($campaign->title), |
| 84 | 'post_date' => $dateCreatedFormatted, |
| 85 | 'post_modified' => $dateUpdatedFormatted, |
| 86 | 'post_status' => $status->getValue(), |
| 87 | 'post_type' => 'page', |
| 88 | 'post_content' => $campaignPage->content ?? give(CreateDefaultLayoutForCampaignPage::class)( |
| 89 | $campaign->id, |
| 90 | $campaign->shortDescription |
| 91 | ), |
| 92 | ]); |
| 93 | |
| 94 | if (!$campaignPage->id) { |
| 95 | throw new Exception('Failed creating a campaign page'); |
| 96 | } |
| 97 | |
| 98 | $campaignPage->createdAt = $dateCreated; |
| 99 | $campaignPage->updatedAt = $dateUpdated; |
| 100 | $campaignPage->status = $status; |
| 101 | |
| 102 | update_post_meta($campaignPage->id, CampaignPageMetaKeys::CAMPAIGN_ID, $campaignPage->campaignId); |
| 103 | |
| 104 | if ($campaign->image && $imageId = attachment_url_to_postid($campaign->image)) { |
| 105 | set_post_thumbnail($campaignPage->id, $imageId); |
| 106 | } |
| 107 | } catch (Exception $exception) { |
| 108 | DB::query('ROLLBACK'); |
| 109 | |
| 110 | Log::error('Failed creating a campaign page', [$campaignPage]); |
| 111 | |
| 112 | throw new $exception('Failed creating a campaign page'); |
| 113 | } |
| 114 | |
| 115 | DB::query('COMMIT'); |
| 116 | |
| 117 | Hooks::doAction('givewp_campaign_page_created', $campaignPage); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @since 4.0.0 |
| 122 | * @throws Exception |
| 123 | */ |
| 124 | public function update(CampaignPage $campaignPage): void |
| 125 | { |
| 126 | $this->validate($campaignPage); |
| 127 | |
| 128 | Hooks::doAction('givewp_campaign_page_updating', $campaignPage); |
| 129 | |
| 130 | $now = Temporal::withoutMicroseconds(Temporal::getCurrentDateTime()); |
| 131 | $nowFormatted = Temporal::getFormattedDateTime($now); |
| 132 | $status = $campaignPage->status ?? CampaignPageStatus::PUBLISH(); |
| 133 | |
| 134 | DB::query('START TRANSACTION'); |
| 135 | |
| 136 | try { |
| 137 | DB::table('posts') |
| 138 | ->where('ID', $campaignPage->id) |
| 139 | ->update([ |
| 140 | 'post_modified' => $nowFormatted, |
| 141 | 'post_modified_gmt' => get_gmt_from_date($nowFormatted), |
| 142 | 'post_status' => $status->getValue(), |
| 143 | 'post_content' => $campaignPage->content, |
| 144 | ]); |
| 145 | |
| 146 | $campaignPage->updatedAt = $now; |
| 147 | $campaignPage->status = $status; |
| 148 | |
| 149 | update_post_meta($campaignPage->id, CampaignPageMetaKeys::CAMPAIGN_ID, $campaignPage->campaignId); |
| 150 | } catch (Exception $exception) { |
| 151 | DB::query('ROLLBACK'); |
| 152 | |
| 153 | Log::error('Failed updating a campaign page', [$campaignPage]); |
| 154 | |
| 155 | throw new $exception('Failed updating a campaign page'); |
| 156 | } |
| 157 | |
| 158 | DB::query('COMMIT'); |
| 159 | |
| 160 | Hooks::doAction('givewp_campaign_page_updated', $campaignPage); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @since 4.0.0 |
| 165 | * @throws Exception |
| 166 | */ |
| 167 | public function delete(CampaignPage $campaignPage): bool |
| 168 | { |
| 169 | DB::query('START TRANSACTION'); |
| 170 | |
| 171 | Hooks::doAction('givewp_campaign_page_deleting', $campaignPage); |
| 172 | |
| 173 | try { |
| 174 | DB::table('posts') |
| 175 | ->where('id', $campaignPage->id) |
| 176 | ->delete(); |
| 177 | |
| 178 | DB::table('postmeta') |
| 179 | ->where('post_id', $campaignPage->id) |
| 180 | ->delete(); |
| 181 | } catch (Exception $exception) { |
| 182 | DB::query('ROLLBACK'); |
| 183 | |
| 184 | Log::error('Failed deleting a campaign page', [$campaignPage]); |
| 185 | |
| 186 | throw new $exception('Failed deleting a campaign page'); |
| 187 | } |
| 188 | |
| 189 | DB::query('COMMIT'); |
| 190 | |
| 191 | Hooks::doAction('givewp_campaign_page_deleted', $campaignPage); |
| 192 | |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @since 4.0.0 |
| 198 | * |
| 199 | * @return ModelQueryBuilder<CampaignPage> |
| 200 | */ |
| 201 | public function prepareQuery(): ModelQueryBuilder |
| 202 | { |
| 203 | $builder = new ModelQueryBuilder(CampaignPage::class); |
| 204 | |
| 205 | return $builder->from('posts') |
| 206 | ->select( |
| 207 | ['ID', 'id'], |
| 208 | ['post_date', 'createdAt'], |
| 209 | ['post_modified', 'updatedAt'], |
| 210 | ['post_status', 'status'], |
| 211 | ['post_content', 'content'] |
| 212 | ) |
| 213 | ->attachMeta( |
| 214 | 'postmeta', |
| 215 | 'ID', |
| 216 | 'post_id', |
| 217 | ...CampaignPageMetaKeys::getColumnsForAttachMetaQuery() |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @since 4.0.0 |
| 223 | */ |
| 224 | public function validate(CampaignPage $campaignPage) |
| 225 | { |
| 226 | foreach ($this->requiredProperties as $key) { |
| 227 | if (!isset($campaignPage->$key)) { |
| 228 | throw new InvalidArgumentException("'$key' is required."); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 |