Skip to content

Commit

Permalink
Fix PHP type issues for plugin settings, not working well with .env v…
Browse files Browse the repository at this point in the history
…ariables
  • Loading branch information
engram-design committed Nov 14, 2024
1 parent dd1b25a commit 417b869
Showing 1 changed file with 44 additions and 15 deletions.
59 changes: 44 additions & 15 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace verbb\abandonedcart\models;

use craft\base\Model;
use craft\behaviors\EnvAttributeParserBehavior;
use craft\helpers\App;
use craft\helpers\StringHelper;

Expand All @@ -12,16 +13,16 @@ class Settings extends Model

public string $pluginName = 'Abandoned Carts';
public ?string $passKey = null;
public ?int $restoreExpiryHours = 48;
public ?int $firstReminderDelay = 1;
public ?int $secondReminderDelay = 12;
public int|string|null $restoreExpiryHours = 48;
public int|string|null $firstReminderDelay = 1;
public int|string|null $secondReminderDelay = 12;
public ?string $discountCode = null;
public ?string $firstReminderTemplate = 'abandoned-cart/emails/first';
public ?string $secondReminderTemplate = 'abandoned-cart/emails/second';
public ?string $firstReminderSubject = 'You‘ve left some items in your cart';
public ?string $secondReminderSubject = 'Your items are still waiting - don‘t miss out';
public ?string $recoveryUrl = 'shop/cart';
public bool $disableSecondReminder = false;
public bool|string|null $disableSecondReminder = false;
public ?string $blacklist = null;


Expand All @@ -45,17 +46,6 @@ public function init(): void
parent::init();
}

public function defineRules(): array
{
$rules = parent::defineRules();
$rules[] = [['pluginName', 'restoreExpiryHours', 'firstReminderDelay', 'secondReminderDelay', 'firstReminderTemplate', 'secondReminderTemplate', 'firstReminderSubject', 'secondReminderSubject', 'recoveryUrl', 'passKey'], 'required'];
$rules[] = ['restoreExpiryHours', 'integer', 'min' => 24, 'max' => '168']; // Atleast 24hrs
$rules[] = ['firstReminderDelay', 'integer', 'min' => 1, 'max' => 24]; // 1hr +
$rules[] = ['secondReminderDelay', 'integer', 'min' => 12, 'max' => 48]; // prevent spam

return $rules;
}

public function getPassKey(): ?string
{
return App::parseEnv($this->passKey);
Expand Down Expand Up @@ -120,4 +110,43 @@ public function getBlacklist(): ?string
{
return App::parseEnv($this->blacklist);
}


// Protected Methods
// =========================================================================

protected function defineRules(): array
{
$rules = parent::defineRules();
$rules[] = [['pluginName', 'restoreExpiryHours', 'firstReminderDelay', 'secondReminderDelay', 'firstReminderTemplate', 'secondReminderTemplate', 'firstReminderSubject', 'secondReminderSubject', 'recoveryUrl', 'passKey'], 'required'];
$rules[] = [['restoreExpiryHours'], 'integer', 'min' => 24, 'max' => '168']; // Atleast 24hrs
$rules[] = [['firstReminderDelay'], 'integer', 'min' => 1, 'max' => 24]; // 1hr +
$rules[] = [['secondReminderDelay'], 'integer', 'min' => 12, 'max' => 48]; // prevent spam

return $rules;
}

protected function defineBehaviors(): array
{
return [
'parser' => [
'class' => EnvAttributeParserBehavior::class,
'attributes' => [
'passKey',
'pluginName',
'disableSecondReminder',
'restoreExpiryHours',
'firstReminderDelay',
'secondReminderDelay',
'firstReminderTemplate',
'firstReminderSubject',
'secondReminderTemplate',
'secondReminderSubject',
'discountCode',
'recoveryUrl',
'blacklist',
],
],
];
}
}

0 comments on commit 417b869

Please sign in to comment.