Skip to content

Commit

Permalink
Feature: Add Campaign model and repository (#7520)
Browse files Browse the repository at this point in the history
Co-authored-by: Kyle B. Johnson
  • Loading branch information
alaca authored Aug 28, 2024
1 parent 3741176 commit b13f5ff
Show file tree
Hide file tree
Showing 13 changed files with 789 additions and 30 deletions.
38 changes: 38 additions & 0 deletions src/Campaigns/Actions/ConvertQueryDataToCampaign.php
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),
]);
}
}
23 changes: 23 additions & 0 deletions src/Campaigns/Actions/DeleteCampaignPage.php
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
}
}
39 changes: 39 additions & 0 deletions src/Campaigns/Factories/CampaignFactory.php
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')),
];
}
}
58 changes: 58 additions & 0 deletions src/Campaigns/Migrations/P2P/SetCampaignType.php
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);
}
}
}
12 changes: 6 additions & 6 deletions src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public function run(): void
{
global $wpdb;

$table = $wpdb->prefix . 'give_campaign_forms';
$table = $wpdb->give_campaign_forms;
$charset = DB::get_charset_collate();

$sql = "CREATE TABLE $table (
campaign_id INT UNSIGNED NOT NULL,
form_id INT UNSIGNED NOT NULL,
PRIMARY KEY (campaign_id),
KEY form_id (form_id)
) $charset";
campaign_id INT UNSIGNED NOT NULL,
form_id INT UNSIGNED NOT NULL,
PRIMARY KEY (campaign_id),
KEY form_id (form_id)
) $charset";

try {
DB::delta($sql);
Expand Down
41 changes: 21 additions & 20 deletions src/Campaigns/Migrations/Tables/CreateCampaignsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CreateCampaignsTable extends Migration
*/
public static function id(): string
{
return 'give-campaigns-create-give-core-campaigns-table';
return 'give-campaigns-create-give-campaigns-table';
}

/**
Expand All @@ -45,28 +45,29 @@ public function run()
{
global $wpdb;

$table = $wpdb->prefix . 'give_campaigns';
$table = $wpdb->give_campaigns;
$charset = DB::get_charset_collate();

$sql = "CREATE TABLE $table (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
form_id INT NOT NULL,
campaign_type VARCHAR(12) NOT NULL DEFAULT '',
campaign_title TEXT NOT NULL,
campaign_url TEXT NOT NULL,
short_desc TEXT NOT NULL,
long_desc TEXT NOT NULL,
campaign_logo TEXT NOT NULL,
campaign_image TEXT NOT NULL,
primary_color VARCHAR(7) NOT NULL,
secondary_color VARCHAR(7) NOT NULL,
campaign_goal INT UNSIGNED NOT NULL,
status VARCHAR(12) NOT NULL,
start_date DATETIME NULL,
end_date DATETIME NULL,
date_created DATETIME NOT NULL,
PRIMARY KEY (id)
) $charset";
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
campaign_page_id INT UNSIGNED NULL,
form_id INT NOT NULL,
campaign_type VARCHAR(12) NOT NULL DEFAULT '',
campaign_title TEXT NOT NULL,
campaign_url TEXT NOT NULL,
short_desc TEXT NOT NULL,
long_desc TEXT NOT NULL,
campaign_logo TEXT NOT NULL,
campaign_image TEXT NOT NULL,
primary_color VARCHAR(7) NOT NULL,
secondary_color VARCHAR(7) NOT NULL,
campaign_goal INT UNSIGNED NOT NULL,
status VARCHAR(12) NOT NULL,
start_date DATETIME NULL,
end_date DATETIME NULL,
date_created DATETIME NOT NULL,
PRIMARY KEY (id)
) $charset";

try {
DB::delta($sql);
Expand Down
136 changes: 136 additions & 0 deletions src/Campaigns/Models/Campaign.php
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);
}
}
Loading

0 comments on commit b13f5ff

Please sign in to comment.