Skip to content

[5.x] Add missing services events #3885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: 5.4
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
- Added `craft\commerce\models\ShippingMethod::setCustomerCondition()`.
- Added `craft\commerce\models\ShippingRule::getCustomerCondition()`.
- Added `craft\commerce\models\ShippingRule::setCustomerCondition()`.
- Added `craft\commerce\services\Gateways::EVENT_AFTER_SAVE_GATEWAY`.
- Added `craft\commerce\services\Gateways::EVENT_BEFORE_SAVE_GATEWAY`.
- Added `craft\commerce\events\GatewayEvent`.

### System
- Added the `resave/variants` command.
- Fixed a bug where gateway settings weren’t storing project config values consistently. ([#3941](https://github.com/craftcms/commerce/issues/3941))

30 changes: 30 additions & 0 deletions src/events/GatewayEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\commerce\events;

use craft\commerce\base\Gateway;
use yii\base\Event;

/**
* Class GatewayEvent
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 5.4.0
*/
class GatewayEvent extends Event
{
/**
* @var Gateway The gateway model associated with the event.
*/
public Gateway $pdf;

/**
* @var bool Whether the Gateway is brand new
*/
public bool $isNew = false;
}
68 changes: 67 additions & 1 deletion src/services/Gateways.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use craft\commerce\base\GatewayInterface;
use craft\commerce\base\SubscriptionGateway;
use craft\commerce\db\Table;
use craft\commerce\events\GatewayEvent;
use craft\commerce\gateways\Dummy;
use craft\commerce\gateways\Manual;
use craft\commerce\gateways\MissingGateway;
Expand All @@ -30,6 +31,7 @@
use Throwable;
use yii\base\Component;
use yii\base\ErrorException;
use yii\base\Event;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\base\NotSupportedException;
Expand All @@ -47,6 +49,56 @@
*/
class Gateways extends Component
{
/**
* @event GatewayEvent The event that is triggered before a gateway is saved.
*
* ```php
* use craft\commerce\events\GatewayEvent;
* use craft\commerce\services\Gateways;
* use craft\commerce\models\Gateway;
* use yii\base\Event;
*
* Event::on(
* Gateways::class,
* Gateways::EVENT_BEFORE_SAVE_GATEWAY,
* function(GatewayEvent $event) {
* // @var Gateway $gateway
* $gateway = $event->gateway;
* // @var bool $isNew
* $isNew = $event->isNew;
*
* // ...
* }
* );
* ```
*/
public const EVENT_BEFORE_SAVE_GATEWAY = 'beforeSaveGateway';

/**
* @event GatewayEvent The event that is triggered after a gateway is saved.
*
* ```php
* use craft\commerce\events\GatewayEvent;
* use craft\commerce\services\Gateways;
* use craft\commerce\models\Gateway;
* use yii\base\Event;
*
* Event::on(
* Gateways::class,
* Gateways::EVENT_AFTER_SAVE_GATEWAY,
* function(GatewayEvent $event) {
* // @var Gateway $gateway
* $gateway = $event->gateway;
* // @var bool $isNew
* $isNew = $event->isNew;
*
* // ...
* }
* );
* ```
*/
public const EVENT_AFTER_SAVE_GATEWAY = 'afterSaveGateway';

/**
* @var array|null Gateway setting overrides
*/
Expand Down Expand Up @@ -234,6 +286,13 @@ public function saveGateway(Gateway $gateway, bool $runValidation = true): bool
{
$isNewGateway = $gateway->getIsNew();

if ($this->hasEventHandlers(self::EVENT_BEFORE_SAVE_GATEWAY)) {
$this->trigger(self::EVENT_BEFORE_SAVE_GATEWAY, new GatewayEvent([
'gateway' => $gateway,
'isNew' => $isNewGateway,
]));
}

if ($runValidation && !$gateway->validate()) {
Craft::info('Gateway not saved due to validation error.', __METHOD__);
return false;
Expand Down Expand Up @@ -285,7 +344,7 @@ public function handleChangedGateway(ConfigEvent $event): void
$transaction = Craft::$app->getDb()->beginTransaction();
try {
$gatewayRecord = $this->_getGatewayRecord($gatewayUid);

$isNewGateway = $gatewayRecord->getIsNewRecord();
$gatewayRecord->name = $data['name'];
$gatewayRecord->handle = $data['handle'];
$gatewayRecord->type = $data['type'];
Expand All @@ -310,6 +369,13 @@ public function handleChangedGateway(ConfigEvent $event): void
$transaction->rollBack();
throw $e;
}

if ($this->hasEventHandlers(self::EVENT_AFTER_SAVE_GATEWAY)) {
$this->trigger(self::EVENT_AFTER_SAVE_GATEWAY, new GatewayEvent([
'gateway' => $this->getGatewayById($gatewayRecord->id),
'isNew' => $isNewGateway,
]));
}
}

/**
Expand Down
Loading