Skip to content

Commit

Permalink
UPDATE: Customize Plan & Charge Models and tables name (gnikyt#966)
Browse files Browse the repository at this point in the history
* add charge and plan model config

* use Plan and Charge model base on config

* prefix config by models

* customize charge and plan tables name

* rolback composer json

* Update all config to be Util::getShopifyConfig

* Fix init class syntax error

Co-authored-by: anass <[email protected]>
  • Loading branch information
ansezz and anass authored Sep 10, 2021
1 parent a2e9bc4 commit d8c61ae
Show file tree
Hide file tree
Showing 29 changed files with 198 additions and 75 deletions.
7 changes: 5 additions & 2 deletions src/Storage/Commands/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Osiset\ShopifyApp\Objects\Values\ChargeReference;
use Osiset\ShopifyApp\Objects\Values\ShopId;
use Osiset\ShopifyApp\Storage\Models\Charge as ChargeModel;
use Osiset\ShopifyApp\Util;

/**
* Represents the commands for charges.
Expand Down Expand Up @@ -49,7 +50,8 @@ public function make(ChargeTransfer $chargeObj): ChargeId
return $obj instanceof Carbon;
};

$charge = new ChargeModel();
$chargeClass = Util::getShopifyConfig('models.charge', ChargeModel::class);
$charge = new $chargeClass();
$charge->plan_id = $chargeObj->planId->toNative();
$charge->user_id = $chargeObj->shopId->toNative();
$charge->charge_id = $chargeObj->chargeReference->toNative();
Expand Down Expand Up @@ -88,7 +90,8 @@ public function delete(ChargeReference $chargeRef, ShopId $shopId): bool
public function makeUsage(UsageChargeTransfer $chargeObj): ChargeId
{
// Create the charge
$charge = new ChargeModel();
$chargeClass = Util::getShopifyConfig('models.charge', ChargeModel::class);
$charge = new $chargeClass();
$charge->user_id = $chargeObj->shopId->toNative();
$charge->charge_id = $chargeObj->chargeReference->toNative();
$charge->type = $chargeObj->chargeType->toNative();
Expand Down
10 changes: 10 additions & 0 deletions src/Storage/Models/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ class Charge extends Model
*/
protected $dates = ['deleted_at'];

/**
* Get table name.
*
* @return string
*/
public function getTable(): string
{
return Util::getShopifyConfig('table_names.charges', parent::getTable());
}

/**
* Get the ID as a value object.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Storage/Models/Plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Osiset\ShopifyApp\Objects\Enums\PlanInterval;
use Osiset\ShopifyApp\Objects\Enums\PlanType;
use Osiset\ShopifyApp\Objects\Values\PlanId;
use Osiset\ShopifyApp\Util;

/**
* Responsible for reprecenting a plan record.
Expand All @@ -25,6 +26,16 @@ class Plan extends Model
'price' => 'float',
];

/**
* Get table name.
*
* @return string
*/
public function getTable(): string
{
return Util::getShopifyConfig('table_names.plans', parent::getTable());
}

/**
* Get the plan ID as a value object.
*
Expand Down
24 changes: 21 additions & 3 deletions src/Storage/Queries/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,36 @@
use Osiset\ShopifyApp\Objects\Values\ChargeReference;
use Osiset\ShopifyApp\Objects\Values\ShopId;
use Osiset\ShopifyApp\Storage\Models\Charge as ChargeModel;
use Osiset\ShopifyApp\Util;

/**
* Represents a queries for charges.
*/
class Charge implements IChargeQuery
{
/**
* the Charge Model.
*
* @var ChargeModel
*/
protected $chargeModel;

/**
* Init for charge command.
*/
public function __construct()
{
$chargeClass = Util::getShopifyConfig('models.charge', ChargeModel::class);
$this->chargeModel = new $chargeClass();
}


/**
* {@inheritdoc}
*/
public function getById(ChargeId $chargeId, array $with = []): ?ChargeModel
{
return ChargeModel::with($with)
return $this->chargeModel->with($with)
->where('id', $chargeId->toNative())
->get()
->first();
Expand All @@ -29,7 +47,7 @@ public function getById(ChargeId $chargeId, array $with = []): ?ChargeModel
*/
public function getByReference(ChargeReference $chargeRef, array $with = []): ?ChargeModel
{
return ChargeModel::with($with)
return $this->chargeModel->with($with)
->where('charge_id', $chargeRef->toNative())
->withTrashed()
->get()
Expand All @@ -41,7 +59,7 @@ public function getByReference(ChargeReference $chargeRef, array $with = []): ?C
*/
public function getByReferenceAndShopId(ChargeReference $chargeRef, ShopId $shopId): ?ChargeModel
{
return ChargeModel::query()
return $this->chargeModel->query()
->where('charge_id', $chargeRef->toNative())
->where('user_id', $shopId->toNative())
->get()
Expand Down
24 changes: 21 additions & 3 deletions src/Storage/Queries/Plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,36 @@
use Osiset\ShopifyApp\Contracts\Objects\Values\PlanId;
use Osiset\ShopifyApp\Contracts\Queries\Plan as IPlanQuery;
use Osiset\ShopifyApp\Storage\Models\Plan as PlanModel;
use Osiset\ShopifyApp\Util;

/**
* Represents plan queries.
*/
class Plan implements IPlanQuery
{
/**
* the Plan Model.
*
* @var PlanModel
*/
protected $planModel;

/**
* Init for charge command.
*/
public function __construct()
{
$chargeClass = Util::getShopifyConfig('models.plan', PlanModel::class);
$this->planModel = new $chargeClass();
}


/**
* {@inheritdoc}
*/
public function getById(PlanId $planId, array $with = []): ?PlanModel
{
return PlanModel::with($with)
return $this->planModel->with($with)
->get()
->where('id', $planId->toNative())
->first();
Expand All @@ -28,7 +46,7 @@ public function getById(PlanId $planId, array $with = []): ?PlanModel
*/
public function getDefault(array $with = []): ?PlanModel
{
return PlanModel::with($with)
return $this->planModel->with($with)
->get()
->where('on_install', true)
->first();
Expand All @@ -39,7 +57,7 @@ public function getDefault(array $with = []): ?PlanModel
*/
public function getAll(array $with = []): Collection
{
return PlanModel::with($with)
return $this->planModel->with($with)
->get();
}
}
6 changes: 5 additions & 1 deletion src/Traits/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Osiset\ShopifyApp\Storage\Models\Plan;
use Osiset\ShopifyApp\Util;

/**
* Responsible for showing the main homescreen for the app.
Expand Down Expand Up @@ -43,6 +44,9 @@ public function getSelf(): JsonResponse
*/
public function getPlans(): JsonResponse
{
return response()->json(Plan::all());
$planClass = Util::getShopifyConfig('models.plan', Plan::class);
$planModel = new $planClass();

return response()->json($planModel->all());
}
}
5 changes: 3 additions & 2 deletions src/Traits/ShopModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Osiset\ShopifyApp\Storage\Models\Charge;
use Osiset\ShopifyApp\Storage\Models\Plan;
use Osiset\ShopifyApp\Storage\Scopes\Namespacing;
use Osiset\ShopifyApp\Util;

/**
* Responsible for representing a shop record.
Expand Down Expand Up @@ -81,7 +82,7 @@ public function getAccessToken(): AccessTokenValue
*/
public function charges(): HasMany
{
return $this->hasMany(Charge::class);
return $this->hasMany(Util::getShopifyConfig('models.charge', Charge::class));
}

/**
Expand All @@ -97,7 +98,7 @@ public function hasCharges(): bool
*/
public function plan(): BelongsTo
{
return $this->belongsTo(Plan::class);
return $this->belongsTo(Util::getShopifyConfig('models.plan', Plan::class));
}

/**
Expand Down
34 changes: 34 additions & 0 deletions src/resources/config/shopify-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,38 @@
*/

'turbo_enabled' => (bool) env('SHOPIFY_TURBO_ENABLED', false),

/*
|--------------------------------------------------------------------------
| Customize Models and Table Name
|--------------------------------------------------------------------------
|
| You can customize you model and extend them
| also you can customize tables name for charge and plan models.
|
*/

'models' => [
/*
* The fully qualified class name of the Charge model.
*/
'charge' => Osiset\ShopifyApp\Storage\Models\Charge::class,

/*
* The fully qualified class name of the Plan model.
*/
'plan' => Osiset\ShopifyApp\Storage\Models\Plan::class,
],

'table_names' => [
/*
* The table name for Charge model.
*/
'charges' => 'charges',

/*
* The table name for Plan model.
*/
'plans' => 'plans',
]
];
17 changes: 10 additions & 7 deletions src/resources/database/factories/ChargeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
use Osiset\ShopifyApp\Objects\Enums\ChargeStatus;
use Osiset\ShopifyApp\Objects\Enums\ChargeType;
use Osiset\ShopifyApp\Storage\Models\Charge;
use Osiset\ShopifyApp\Util;

$factory->define(Charge::class, function (Faker $faker) {
$chargeModel = Util::getShopifyConfig('models.charge', Charge::class);

$factory->define($chargeModel, function (Faker $faker) {
return [
'charge_id' => $faker->randomNumber(8),
'name' => $faker->word,
Expand All @@ -15,27 +18,27 @@
];
});

$factory->state(Charge::class, 'test', [
$factory->state($chargeModel, 'test', [
'test' => true,
]);

$factory->state(Charge::class, 'type_recurring', [
$factory->state($chargeModel, 'type_recurring', [
'type' => ChargeType::RECURRING()->toNative(),
]);

$factory->state(Charge::class, 'type_onetime', [
$factory->state($chargeModel, 'type_onetime', [
'type' => ChargeType::CHARGE()->toNative(),
]);

$factory->state(Charge::class, 'type_usage', [
$factory->state($chargeModel, 'type_usage', [
'type' => ChargeType::USAGE()->toNative(),
]);

$factory->state(Charge::class, 'type_credit', [
$factory->state($chargeModel, 'type_credit', [
'type' => ChargeType::CREDIT()->toNative(),
]);

$factory->state(Charge::class, 'trial', function ($faker) {
$factory->state($chargeModel, 'trial', function ($faker) {
$days = $faker->numberBetween(7, 14);

return [
Expand Down
19 changes: 11 additions & 8 deletions src/resources/database/factories/PlanFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,47 @@
use Osiset\ShopifyApp\Objects\Enums\PlanInterval;
use Osiset\ShopifyApp\Objects\Enums\PlanType;
use Osiset\ShopifyApp\Storage\Models\Plan;
use Osiset\ShopifyApp\Util;

$factory->define(Plan::class, function (Faker $faker) {
$planModel = Util::getShopifyConfig('models.plan', Plan::class);

$factory->define($planModel, function (Faker $faker) {
return [
'name' => $faker->word,
'price' => $faker->randomFloat(),
];
});

$factory->state(Plan::class, 'usage', function ($faker) {
$factory->state($planModel, 'usage', function ($faker) {
return [
'capped_amount' => $faker->randomFloat(),
'terms' => $faker->sentence,
];
});

$factory->state(Plan::class, 'trial', function ($faker) {
$factory->state($planModel, 'trial', function ($faker) {
return [
'trial_days' => $faker->numberBetween(7, 14),
];
});

$factory->state(Plan::class, 'test', [
$factory->state($planModel, 'test', [
'test' => true,
]);

$factory->state(Plan::class, 'installable', [
$factory->state($planModel, 'installable', [
'on_install' => true,
]);

$factory->state(Plan::class, 'type_recurring', [
$factory->state($planModel, 'type_recurring', [
'type' => PlanType::RECURRING()->toNative(),
'interval' => PlanInterval::EVERY_30_DAYS()->toNative(),
]);

$factory->state(Plan::class, 'type_onetime', [
$factory->state($planModel, 'type_onetime', [
'type' => PlanType::ONETIME()->toNative(),
]);

$factory->state(Plan::class, 'interval_annual', [
$factory->state($planModel, 'interval_annual', [
'interval' => PlanInterval::ANNUAL()->toNative(),
]);
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Osiset\ShopifyApp\Util;

class CreatePlansTable extends Migration
{
Expand All @@ -13,7 +14,7 @@ class CreatePlansTable extends Migration
*/
public function up()
{
Schema::create('plans', function (Blueprint $table) {
Schema::create(Util::getShopifyConfig('table_names.plans', 'plans'), function (Blueprint $table) {
$table->increments('id');

// The type of plan, either PlanType::RECURRING (0) or PlanType::ONETIME (1)
Expand Down Expand Up @@ -52,6 +53,6 @@ public function up()
*/
public function down()
{
Schema::drop('plans');
Schema::drop(Util::getShopifyConfig('table_names.plans', 'plans'));
}
}
Loading

0 comments on commit d8c61ae

Please sign in to comment.