Skip to content

Commit

Permalink
Fixed a couple PHPStan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Apr 30, 2024
1 parent 1bbc417 commit 5060945
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use craft\controllers\UsersController;
use craft\elements\conditions\users\UserCondition;
use craft\elements\User;
use craft\elements\User as UserElement;
use craft\events\DefineBehaviorsEvent;
use craft\events\DefineConsoleActionsEvent;
use craft\events\DefineEditUserScreensEvent;
Expand Down Expand Up @@ -507,8 +506,8 @@ private function registerSiteRoutes(): void
private function registerBehaviors(): void
{
Event::on(
UserElement::class,
UserElement::EVENT_DEFINE_BEHAVIORS,
User::class,
User::EVENT_DEFINE_BEHAVIORS,
function(DefineBehaviorsEvent $event) {
$event->behaviors['stripe:customer'] = StripeCustomerBehavior::class;
}
Expand Down Expand Up @@ -539,16 +538,17 @@ private function handleUserElementChanges(): void
// if email address got changed - update stripe
Event::on(UserRecord::class, UserRecord::EVENT_BEFORE_UPDATE, function(ModelEvent $event) {
$userRecord = $event->sender;
/** @var User|StripeCustomerBehavior $user */
$user = Craft::$app->getUsers()->getUserById($userRecord->id);
$settings = $this->getSettings();
if ($user->isCredentialed && $settings['syncChangedUserEmailsToStripe']) {
$oldEmail = $userRecord->getOldAttribute('email');
$newEmail = $userRecord->getAttribute('email');
if ($oldEmail != $newEmail) {
$customers = $user->getStripeCustomers();
if (!empty($customers)) {
if ($customers->isNotEmpty()) {
$client = $this->getApi()->getClient();
foreach ($customers as $customer) {
foreach ($customers->all() as $customer) {
$client->customers->update($customer->stripeId, ['email' => $newEmail]);
}
}
Expand All @@ -559,6 +559,7 @@ private function handleUserElementChanges(): void
// if user is saved, and they have an email address and exist in stripe, but we don't have their stripe customer data
// kick off queue job to sync customer-related data
Event::on(User::class, User::EVENT_AFTER_SAVE, function(ModelEvent $event) {
/** @var User|StripeCustomerBehavior $user */
$user = $event->sender;
if (!empty($user->email) && $user->getStripeCustomers()->isEmpty()) {
// search for customer in Stripe by their email address
Expand Down

0 comments on commit 5060945

Please sign in to comment.