Skip to content

Commit f328e68

Browse files
Cyperghostdtdesign
andauthored
Migrate the PaidSubscriptionUserAction functions to commands (#6428)
* Migrate the `PaidSubscriptionUserAction` functions to commands * Check if the subscription is active not in the command * Minor fixes --------- Co-authored-by: Alexander Ebert <ebert@woltlab.com>
1 parent 9810b9e commit f328e68

12 files changed

Lines changed: 405 additions & 101 deletions

wcfsetup/install/files/lib/acp/form/PaidSubscriptionUserAddForm.class.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace wcf\acp\form;
44

5+
use wcf\command\paid\subscription\user\ExtendPaidSubscriptionUser;
56
use wcf\data\paid\subscription\PaidSubscription;
67
use wcf\data\paid\subscription\user\PaidSubscriptionUser;
78
use wcf\data\paid\subscription\user\PaidSubscriptionUserAction;
@@ -169,9 +170,10 @@ public function save()
169170
]);
170171
$this->objectAction->executeAction();
171172
} else {
172-
// extend existing subscription
173-
$this->objectAction = new PaidSubscriptionUserAction([$userSubscription], 'extend', ['data' => $data]);
174-
$this->objectAction->executeAction();
173+
(new ExtendPaidSubscriptionUser(
174+
$userSubscription,
175+
$data['endDate'] ?? null,
176+
))();
175177
}
176178
$this->saved();
177179

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace wcf\command\paid\subscription;
4+
5+
use wcf\data\paid\subscription\PaidSubscription;
6+
use wcf\data\user\group\UserGroup;
7+
use wcf\data\user\User;
8+
use wcf\data\user\UserAction;
9+
10+
/**
11+
* Adds user groups to a user based on the paid subscription's associated group IDs.
12+
*
13+
* @author Olaf Braun
14+
* @copyright 2001-2025 WoltLab GmbH
15+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16+
* @since 6.3
17+
*/
18+
final class AddGroupMembership
19+
{
20+
public function __construct(
21+
private readonly PaidSubscription $subscription,
22+
private readonly User $user,
23+
) {}
24+
25+
public function __invoke(): void
26+
{
27+
$groupIDs = $this->getGroupIDs($this->subscription);
28+
29+
if ($groupIDs === []) {
30+
return;
31+
}
32+
33+
(new UserAction([$this->user], 'addToGroups', [
34+
'groups' => $groupIDs,
35+
'deleteOldGroups' => false,
36+
'addDefaultGroups' => false,
37+
]))->executeAction();
38+
}
39+
40+
/**
41+
* @return list<int>
42+
*/
43+
private function getGroupIDs(PaidSubscription $subscription): array
44+
{
45+
$groupIDs = [];
46+
47+
foreach (\explode(',', $subscription->groupIDs) as $groupID) {
48+
$groupID = (int)$groupID;
49+
if (UserGroup::getGroupByID($groupID) !== null) {
50+
$groupIDs[] = $groupID;
51+
}
52+
}
53+
54+
return $groupIDs;
55+
}
56+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace wcf\command\paid\subscription;
4+
5+
use wcf\data\paid\subscription\PaidSubscription;
6+
use wcf\data\user\group\UserGroup;
7+
use wcf\data\user\User;
8+
use wcf\data\user\UserAction;
9+
10+
/**
11+
* Removes user groups from a user based on the paid subscription's associated group IDs.
12+
*
13+
* @author Olaf Braun
14+
* @copyright 2001-2025 WoltLab GmbH
15+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16+
* @since 6.3
17+
*/
18+
final class RemoveGroupMembership
19+
{
20+
public function __construct(
21+
private readonly PaidSubscription $subscription,
22+
private readonly User $user,
23+
) {}
24+
25+
public function __invoke(): void
26+
{
27+
$groupIDs = $this->getGroupIDs($this->subscription);
28+
29+
if ($groupIDs === []) {
30+
return;
31+
}
32+
33+
(new UserAction([$this->user], 'removeFromGroups', [
34+
'groups' => $groupIDs,
35+
]))->executeAction();
36+
}
37+
38+
/**
39+
* @return list<int>
40+
*/
41+
private function getGroupIDs(PaidSubscription $subscription): array
42+
{
43+
$groupIDs = [];
44+
45+
foreach (\explode(',', $subscription->groupIDs) as $groupID) {
46+
$groupID = (int)$groupID;
47+
if (UserGroup::getGroupByID($groupID) !== null) {
48+
$groupIDs[] = $groupID;
49+
}
50+
}
51+
52+
return $groupIDs;
53+
}
54+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace wcf\command\paid\subscription\user;
4+
5+
use wcf\command\paid\subscription\AddGroupMembership;
6+
use wcf\data\paid\subscription\PaidSubscription;
7+
use wcf\data\paid\subscription\user\PaidSubscriptionUser;
8+
use wcf\data\paid\subscription\user\PaidSubscriptionUserEditor;
9+
use wcf\event\paid\subscription\user\PaidSubscriptionUserExtended;
10+
use wcf\system\event\EventHandler;
11+
use wcf\util\DateUtil;
12+
13+
/**
14+
* Extends the period of a paid subscription for a user.
15+
*
16+
* @author Olaf Braun
17+
* @copyright 2001-2025 WoltLab GmbH
18+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19+
* @since 6.3
20+
*/
21+
final class ExtendPaidSubscriptionUser
22+
{
23+
public function __construct(
24+
private readonly PaidSubscriptionUser $subscriptionUser,
25+
private readonly ?int $endDate = null,
26+
) {}
27+
28+
public function __invoke(): void
29+
{
30+
$endDate = $this->getEndDate($this->subscriptionUser->getSubscription(), $this->endDate);
31+
32+
$this->extendSubscription($this->subscriptionUser, $endDate);
33+
34+
if (!$this->subscriptionUser->isActive) {
35+
(new AddGroupMembership(
36+
$this->subscriptionUser->getSubscription(),
37+
$this->subscriptionUser->getUser()
38+
))();
39+
}
40+
41+
$event = new PaidSubscriptionUserExtended($this->subscriptionUser, $endDate);
42+
EventHandler::getInstance()->fire($event);
43+
}
44+
45+
private function getEndDate(PaidSubscription $subscription, ?int $endDate): int
46+
{
47+
if ($endDate !== null) {
48+
return $endDate;
49+
}
50+
51+
if (!$subscription->subscriptionLength) {
52+
return 0;
53+
}
54+
55+
$d = DateUtil::getDateTimeByTimestamp(\TIME_NOW);
56+
$d->add($subscription->getDateInterval());
57+
58+
return $d->getTimestamp();
59+
}
60+
61+
private function extendSubscription(PaidSubscriptionUser $subscriptionUser, int $endDate): void
62+
{
63+
(new PaidSubscriptionUserEditor($subscriptionUser))->update([
64+
'endDate' => $endDate,
65+
'isActive' => 1,
66+
'sentExpirationNotification' => 0,
67+
]);
68+
}
69+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace wcf\command\paid\subscription\user;
4+
5+
use wcf\command\paid\subscription\AddGroupMembership;
6+
use wcf\data\paid\subscription\user\PaidSubscriptionUser;
7+
use wcf\data\paid\subscription\user\PaidSubscriptionUserEditor;
8+
use wcf\event\paid\subscription\user\PaidSubscriptionUserRestored;
9+
use wcf\system\event\EventHandler;
10+
11+
/**
12+
* Restores a paid subscription for a user.
13+
*
14+
* @author Olaf Braun
15+
* @copyright 2001-2025 WoltLab GmbH
16+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17+
* @since 6.3
18+
*/
19+
final class RestorePaidSubscriptionUser
20+
{
21+
public function __construct(
22+
private readonly PaidSubscriptionUser $subscriptionUser,
23+
) {}
24+
25+
public function __invoke(): void
26+
{
27+
$user = $this->subscriptionUser->getUser();
28+
$subscription = $this->subscriptionUser->getSubscription();
29+
30+
$this->restoreSubscription($this->subscriptionUser);
31+
32+
(new AddGroupMembership($subscription, $user))();
33+
34+
$event = new PaidSubscriptionUserRestored($this->subscriptionUser);
35+
EventHandler::getInstance()->fire($event);
36+
}
37+
38+
private function restoreSubscription(PaidSubscriptionUser $subscriptionUser): void
39+
{
40+
(new PaidSubscriptionUserEditor($subscriptionUser))->update(['isActive' => 1]);
41+
}
42+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace wcf\command\paid\subscription\user;
4+
5+
use wcf\command\paid\subscription\AddGroupMembership;
6+
use wcf\command\paid\subscription\RemoveGroupMembership;
7+
use wcf\data\paid\subscription\PaidSubscription;
8+
use wcf\data\paid\subscription\user\PaidSubscriptionUser;
9+
use wcf\data\paid\subscription\user\PaidSubscriptionUserEditor;
10+
use wcf\event\paid\subscription\user\PaidSubscriptionUserRevoked;
11+
use wcf\system\event\EventHandler;
12+
use wcf\system\WCF;
13+
14+
/**
15+
* Revokes a paid subscription for a user.
16+
*
17+
* @author Olaf Braun
18+
* @copyright 2001-2025 WoltLab GmbH
19+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20+
* @since 6.3
21+
*/
22+
final class RevokePaidSubscriptionUser
23+
{
24+
public function __construct(
25+
private readonly PaidSubscriptionUser $subscriptionUser,
26+
) {}
27+
28+
public function __invoke(): void
29+
{
30+
$this->revokeSubscription($this->subscriptionUser);
31+
$user = $this->subscriptionUser->getUser();
32+
33+
(new RemoveGroupMembership($this->subscriptionUser->getSubscription(), $user))();
34+
35+
foreach ($this->getActiveSubscriptions($this->subscriptionUser->userID) as $activeSubscription) {
36+
(new AddGroupMembership($activeSubscription, $user))();
37+
}
38+
39+
$event = new PaidSubscriptionUserRevoked($this->subscriptionUser);
40+
EventHandler::getInstance()->fire($event);
41+
}
42+
43+
private function revokeSubscription(PaidSubscriptionUser $subscriptionUser): void
44+
{
45+
(new PaidSubscriptionUserEditor($subscriptionUser))->update(['isActive' => 0]);
46+
}
47+
48+
/**
49+
* @return PaidSubscription[]
50+
*/
51+
private function getActiveSubscriptions(int $userID): array
52+
{
53+
$sql = "SELECT *
54+
FROM wcf1_paid_subscription
55+
WHERE subscriptionID IN (
56+
SELECT subscriptionID
57+
FROM wcf1_paid_subscription_user
58+
WHERE userID = ?
59+
AND isActive = ?
60+
)";
61+
$statement = WCF::getDB()->prepare($sql);
62+
$statement->execute([
63+
$userID,
64+
1,
65+
]);
66+
67+
return $statement->fetchObjects(PaidSubscription::class);
68+
}
69+
}

0 commit comments

Comments
 (0)