1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Copyright (C) 2020-2025 Iain Cambridge |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by |
10
|
|
|
* the Free Software Foundation, either version 2.1 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Lesser General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Parthenon\Payments\Command; |
23
|
|
|
|
24
|
|
|
use Parthenon\Common\LoggerAwareTrait; |
25
|
|
|
use Parthenon\Payments\Entity\Subscription; |
26
|
|
|
use Parthenon\Payments\Repository\SubscriberRepositoryInterface; |
27
|
|
|
use Parthenon\Payments\SubscriptionManagerInterface; |
28
|
|
|
use Parthenon\Payments\Transition\ToActiveManagerInterface; |
29
|
|
|
use Parthenon\Payments\Transition\ToCancelledManagerInterface; |
30
|
|
|
use Parthenon\Payments\Transition\ToOverdueManagerInterface; |
31
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
32
|
|
|
use Symfony\Component\Console\Command\Command; |
33
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
34
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
35
|
|
|
|
36
|
|
|
#[AsCommand(name: 'parthenon:payments:sync-subscriptions', description: 'Sync Subscriptions')] |
37
|
|
|
class SyncSubscriptionsCommand extends Command |
38
|
|
|
{ |
39
|
|
|
use LoggerAwareTrait; |
40
|
|
|
|
41
|
|
|
public function __construct( |
42
|
|
|
private SubscriberRepositoryInterface $subscriberRepository, |
43
|
|
|
private SubscriptionManagerInterface $subscriptionManager, |
44
|
|
|
private ToActiveManagerInterface $activeManager, |
45
|
|
|
private ToCancelledManagerInterface $cancelledManager, |
46
|
|
|
private ToOverdueManagerInterface $overdueManager, |
47
|
|
|
) { |
48
|
|
|
parent::__construct(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
52
|
|
|
{ |
53
|
|
|
$subscribers = $this->subscriberRepository->findAllSubscriptions(); |
54
|
|
|
|
55
|
|
|
foreach ($subscribers as $subscriber) { |
56
|
|
|
$oldStatus = $subscriber->getSubscription()->getStatus(); |
57
|
|
|
|
58
|
|
|
$this->subscriptionManager->syncStatus($subscriber->getSubscription()); |
|
|
|
|
59
|
|
|
|
60
|
|
|
if ($oldStatus !== $subscriber->getSubscription()->getStatus()) { |
61
|
|
|
switch ($subscriber->getSubscription()->getStatus()) { |
62
|
|
|
case Subscription::STATUS_ACTIVE: |
63
|
|
|
$this->activeManager->transition($subscriber); |
64
|
|
|
break; |
65
|
|
|
case Subscription::STATUS_CANCELLED: |
66
|
|
|
$this->cancelledManager->transition($subscriber); |
67
|
|
|
break; |
68
|
|
|
case Subscription::STATUS_OVERDUE: |
69
|
|
|
$this->cancelledManager->transition($subscriber); |
70
|
|
|
break; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->subscriberRepository->save($subscriber); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return Command::SUCCESS; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|