/
ProcessesSubscriptions.php
168 lines (138 loc) · 4.84 KB
/
ProcessesSubscriptions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace TeamGantt\Dues\Processor;
use TeamGantt\Dues\Contracts\SubscriptionGateway;
use TeamGantt\Dues\Event\Dispatcher;
use TeamGantt\Dues\Event\EventType;
use TeamGantt\Dues\Model\Customer;
use TeamGantt\Dues\Model\Customer\CustomerSession;
use TeamGantt\Dues\Model\Modifier\AddOn;
use TeamGantt\Dues\Model\Modifier\Discount;
use TeamGantt\Dues\Model\PaymentMethod;
use TeamGantt\Dues\Model\Plan;
use TeamGantt\Dues\Model\Subscription;
use TeamGantt\Dues\Model\Subscription\Status;
use TeamGantt\Dues\Model\Transaction;
use TeamGantt\Dues\Processor\Braintree\Query\SubscriptionQuery;
/**
* This trait exists to provide simple delegation to a SubscriptionGateway.
*/
trait ProcessesSubscriptions
{
private SubscriptionGateway $gateway;
private Dispatcher $events;
public function createCustomer(Customer $customer): Customer
{
$this->events->dispatch(EventType::beforeCreateCustomer(), $customer);
$result = $this->gateway->createCustomer($customer);
$this->events->dispatch(EventType::afterCreateCustomer(), $result);
return $result;
}
public function updateCustomer(Customer $customer): Customer
{
$this->events->dispatch(EventType::beforeUpdateCustomer(), $customer);
$result = $this->gateway->updateCustomer($customer);
$this->events->dispatch(EventType::afterUpdateCustomer(), $result);
return $result;
}
public function deleteCustomer(string $customerId): void
{
$this->gateway->deleteCustomer($customerId);
}
public function cancelSubscription(string $subscriptionId): Subscription
{
return $this->gateway->cancelSubscription($subscriptionId);
}
/**
* @param Subscription[] $subscriptions
*
* @return Subscription[]
*/
public function cancelSubscriptions(array $subscriptions): array
{
return $this->gateway->cancelSubscriptions($subscriptions);
}
public function createPaymentMethod(PaymentMethod $paymentMethod): PaymentMethod
{
return $this->gateway->createPaymentMethod($paymentMethod);
}
public function createSubscription(Subscription $subscription): Subscription
{
$result = $this->gateway->createSubscription($subscription);
$this->events->dispatch(EventType::afterCreateSubscription(), $result);
return $result;
}
public function updateSubscription(Subscription $subscription): Subscription
{
$this->events->dispatch(EventType::beforeUpdateSubscription(), $subscription);
$result = $this->gateway->updateSubscription($subscription);
$this->events->dispatch(EventType::afterUpdateSubscription(), $result);
return $result;
}
public function findCustomerById(string $customerId): ?Customer
{
return $this->gateway->findCustomerById($customerId);
}
/**
* @param Status[] $statuses
*
* @return Subscription[]
*/
public function findSubscriptionsByCustomerId(string $customerId, array $statuses = []): array
{
return $this->gateway->findSubscriptionsByCustomerId($customerId, $statuses);
}
public function findSubscriptionById(string $subscriptionId): ?Subscription
{
return $this->gateway->findSubscriptionById($subscriptionId);
}
public function createCustomerSession(?string $customerId = null): CustomerSession
{
return $this->gateway->createCustomerSession($customerId);
}
public function findTransactionById(string $transactionId): ?Transaction
{
return $this->gateway->findTransactionById($transactionId);
}
/**
* @return Transaction[]
*/
public function findTransactionsByCustomerId(string $customerId, ?\DateTime $start = null, ?\DateTime $end = null): array
{
return $this->gateway->findTransactionsByCustomerId($customerId, $start, $end);
}
/**
* @return Transaction[]
*/
public function findTransactionsBySubscriptionId(string $subscriptionId): array
{
return $this->gateway->findTransactionsBySubscriptionId($subscriptionId);
}
public function listAddOns(): array
{
return $this->gateway->listAddOns();
}
public function findAddOnById(string $id): ?AddOn
{
return $this->gateway->findAddOnById($id);
}
public function listDiscounts(): array
{
return $this->gateway->listDiscounts();
}
public function findDiscountById(string $id): ?Discount
{
return $this->gateway->findDiscountById($id);
}
public function listPlans(): array
{
return $this->gateway->listPlans();
}
public function findPlanById(string $planId): ?Plan
{
return $this->gateway->findPlanById($planId);
}
public function makeSubscriptionQuery(): SubscriptionQuery
{
return $this->gateway->makeSubscriptionQuery();
}
}