SubscriptionManager   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 56
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A cancel() 0 3 1
A getInvoiceUrl() 0 16 5
A syncStatus() 0 17 3
A change() 0 2 1
A getBillingPortal() 0 3 1
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\PaymentProvider\TransactionCloud;
23
24
use Parthenon\Payments\Entity\Subscription;
25
use Parthenon\Payments\SubscriptionManagerInterface;
26
use TransactionCloud\Model\PaymentEntry;
0 ignored issues
show
Bug introduced by
The type TransactionCloud\Model\PaymentEntry was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use TransactionCloud\TransactionCloud;
0 ignored issues
show
Bug introduced by
The type TransactionCloud\TransactionCloud was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
29
class SubscriptionManager implements SubscriptionManagerInterface
30
{
31
    public function __construct(private TransactionCloud $transactionCloud)
32
    {
33
    }
34
35
    public function cancel(Subscription $subscription)
36
    {
37
        $this->transactionCloud->cancelSubscription($subscription->getPaymentId());
38
    }
39
40
    public function change(Subscription $subscription)
41
    {
42
        // TODO: Implement change() method.
43
    }
44
45
    public function syncStatus(Subscription $subscription): Subscription
46
    {
47
        $transaction = $this->transactionCloud->getTransactionById($subscription->getPaymentId());
48
        $remoteStatus = $transaction->getTransactionStatus();
49
50
        switch ($remoteStatus) {
51
            case 'SUBSCRIPTION_STATUS_CANCELLED_PENDING':
52
            case 'SUBSCRIPTION_STATUS_CANCELLED':
53
                $status = Subscription::STATUS_CANCELLED;
54
                break;
55
            default:
56
                $status = Subscription::STATUS_ACTIVE;
57
        }
58
59
        $subscription->setStatus($status);
60
61
        return $subscription;
62
    }
63
64
    public function getInvoiceUrl(Subscription $subscription)
65
    {
66
        $transaction = $this->transactionCloud->getTransactionById($subscription->getPaymentId());
67
        /** @var PaymentEntry $payment */
68
        $payment = null;
69
        foreach ($transaction->getEntries() as $entry) {
70
            if (null === $payment || $payment->getCreateDate() < $entry->getCreateDate()) {
71
                $payment = $entry;
72
            }
73
        }
74
75
        if (null === $payment) {
76
            return null;
77
        }
78
79
        return $this->transactionCloud->getInvoiceUrlForPayment($entry);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $entry seems to be defined by a foreach iteration on line 69. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
80
    }
81
82
    public function getBillingPortal(Subscription $subscription): string
83
    {
84
        return $this->transactionCloud->getUrlToManageTransactions($subscription->getCustomerId());
85
    }
86
}
87