-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add Campaign model and repository (#7520)
Co-authored-by: Kyle B. Johnson
- Loading branch information
Showing
13 changed files
with
789 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Give\Campaigns\Actions; | ||
|
||
use Give\Campaigns\Models\Campaign; | ||
use Give\Campaigns\ValueObjects\CampaignStatus; | ||
use Give\Campaigns\ValueObjects\CampaignType; | ||
use Give\Framework\Support\Facades\DateTime\Temporal; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
class ConvertQueryDataToCampaign | ||
{ | ||
/** | ||
* @unreleased | ||
*/ | ||
public function __invoke(object $queryObject): Campaign | ||
{ | ||
return new Campaign([ | ||
'id' => (int)$queryObject->id, | ||
'pageId' => (int)$queryObject->pageId, | ||
'type' => new CampaignType($queryObject->type), | ||
'title' => $queryObject->title, | ||
'shortDescription' => $queryObject->shortDescription, | ||
'longDescription' => $queryObject->longDescription, | ||
'logo' => $queryObject->logo, | ||
'image' => $queryObject->image, | ||
'primaryColor' => $queryObject->primaryColor, | ||
'secondaryColor' => $queryObject->secondaryColor, | ||
'goal' => (int)$queryObject->goal, | ||
'startDate' => Temporal::toDateTime($queryObject->startDate), | ||
'endDate' => Temporal::toDateTime($queryObject->endDate), | ||
'status' => new CampaignStatus($queryObject->status), | ||
'createdAt' => Temporal::toDateTime($queryObject->createdAt), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Give\Campaigns\Actions; | ||
|
||
use Give\Campaigns\Models\Campaign; | ||
|
||
/** | ||
* @unreleased | ||
* | ||
* Deletes campaign page when the campaign is deleted | ||
* | ||
* @event givewp_campaign_deleted | ||
*/ | ||
class DeleteCampaignPage | ||
{ | ||
/** | ||
* @unreleased | ||
*/ | ||
public function __invoke(Campaign $campaign): void | ||
{ | ||
// todo: delete the campaign page | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Give\Campaigns\Factories; | ||
|
||
use Give\Campaigns\ValueObjects\CampaignStatus; | ||
use Give\Campaigns\ValueObjects\CampaignType; | ||
use Give\Framework\Models\Factories\ModelFactory; | ||
use Give\Framework\Support\Facades\DateTime\Temporal; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
class CampaignFactory extends ModelFactory | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function definition(): array | ||
{ | ||
$currentDate = Temporal::getCurrentDateTime(); | ||
|
||
return [ | ||
'pageId' => 1, | ||
'type' => CampaignType::CORE(), | ||
'title' => __('GiveWP Campaign', 'give'), | ||
'shortDescription' => __('Campaign short description', 'give'), | ||
'longDescription' => __('Campaign long description', 'give'), | ||
'goal' => 10000000, | ||
'status' => CampaignStatus::ACTIVE(), | ||
'logo' => '', | ||
'image' => '', | ||
'primaryColor' => '#28C77B', | ||
'secondaryColor' => '#FFA200', | ||
'createdAt' => Temporal::withoutMicroseconds($currentDate), | ||
'startDate' => Temporal::withoutMicroseconds($currentDate), | ||
'endDate' => Temporal::withoutMicroseconds($currentDate->modify('+1 day')), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Give\Campaigns\Migrations\P2P; | ||
|
||
use Give\Campaigns\ValueObjects\CampaignType; | ||
use Give\Framework\Database\DB; | ||
use Give\Framework\Database\Exceptions\DatabaseQueryException; | ||
use Give\Framework\Migrations\Contracts\Migration; | ||
use Give\Framework\Migrations\Exceptions\DatabaseMigrationException; | ||
|
||
/** | ||
* @unreleased | ||
* | ||
* Set campaign type for existing P2P campaign | ||
*/ | ||
class SetCampaignType extends Migration | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public static function id(): string | ||
{ | ||
return 'give-campaigns-set-campaign-type'; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public static function title(): string | ||
{ | ||
return 'Set campaign type for existing P2P campaigns'; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public static function timestamp(): string | ||
{ | ||
return strtotime('2024-08-26 00:00:02'); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @throws DatabaseMigrationException | ||
*/ | ||
public function run() | ||
{ | ||
try { | ||
DB::table('give_campaigns') | ||
->where('campaign_type', '') | ||
->update([ | ||
'campaign_type' => CampaignType::PEER_TO_PEER | ||
]); | ||
} catch (DatabaseQueryException $exception) { | ||
throw new DatabaseMigrationException('An error occurred while updating the campaign type', 0, $exception); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
namespace Give\Campaigns\Models; | ||
|
||
use DateTime; | ||
use Give\Campaigns\Actions\ConvertQueryDataToCampaign; | ||
use Give\Campaigns\Factories\CampaignFactory; | ||
use Give\Campaigns\Repositories\CampaignRepository; | ||
use Give\Campaigns\ValueObjects\CampaignStatus; | ||
use Give\Campaigns\ValueObjects\CampaignType; | ||
use Give\Framework\Exceptions\Primitives\Exception; | ||
use Give\Framework\Exceptions\Primitives\InvalidArgumentException; | ||
use Give\Framework\Models\Contracts\ModelCrud; | ||
use Give\Framework\Models\Contracts\ModelHasFactory; | ||
use Give\Framework\Models\Model; | ||
use Give\Framework\Models\ModelQueryBuilder; | ||
|
||
/** | ||
* @unreleased | ||
* | ||
* @property int $id | ||
* @property int $pageId | ||
* @property CampaignType $type | ||
* @property string $title | ||
* @property string $url | ||
* @property string $shortDescription | ||
* @property string $longDescription | ||
* @property string $logo | ||
* @property string $image | ||
* @property string $primaryColor | ||
* @property string $secondaryColor | ||
* @property int $goal | ||
* @property CampaignStatus $status | ||
* @property DateTime $startDate | ||
* @property DateTime $endDate | ||
* @property DateTime $createdAt | ||
*/ | ||
class Campaign extends Model implements ModelCrud, ModelHasFactory | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
protected $properties = [ | ||
'id' => 'int', | ||
'pageId' => 'int', | ||
'type' => CampaignType::class, | ||
'title' => 'string', | ||
'shortDescription' => 'string', | ||
'longDescription' => 'string', | ||
'logo' => 'string', | ||
'image' => 'string', | ||
'primaryColor' => 'string', | ||
'secondaryColor' => 'string', | ||
'goal' => 'int', | ||
'status' => CampaignStatus::class, | ||
'startDate' => DateTime::class, | ||
'endDate' => DateTime::class, | ||
'createdAt' => DateTime::class, | ||
]; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public static function factory(): CampaignFactory | ||
{ | ||
return new CampaignFactory(static::class); | ||
} | ||
|
||
/** | ||
* Find campaign by ID | ||
* | ||
* @unreleased | ||
*/ | ||
public static function find($id): ?Campaign | ||
{ | ||
return give(CampaignRepository::class)->getById($id); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
* | ||
* @throws Exception | ||
*/ | ||
public static function create(array $attributes): Campaign | ||
{ | ||
$campaign = new static($attributes); | ||
|
||
give(CampaignRepository::class)->insert($campaign); | ||
|
||
return $campaign; | ||
} | ||
|
||
/** | ||
* @unreleased | ||
* | ||
* @throws Exception|InvalidArgumentException | ||
*/ | ||
public function save(): void | ||
{ | ||
if ( ! $this->id) { | ||
give(CampaignRepository::class)->insert($this); | ||
} else { | ||
give(CampaignRepository::class)->update($this); | ||
} | ||
} | ||
|
||
/** | ||
* @unreleased | ||
* | ||
* @throws Exception | ||
*/ | ||
public function delete(): bool | ||
{ | ||
return give(CampaignRepository::class)->delete($this); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
* | ||
* @return ModelQueryBuilder<Campaign> | ||
*/ | ||
public static function query(): ModelQueryBuilder | ||
{ | ||
return give(CampaignRepository::class)->prepareQuery(); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
* | ||
* @param object $object | ||
*/ | ||
public static function fromQueryBuilderObject($object): Campaign | ||
{ | ||
return (new ConvertQueryDataToCampaign())($object); | ||
} | ||
} |
Oops, something went wrong.