-
Notifications
You must be signed in to change notification settings - Fork 24
/
SubscriptionStarted.php
53 lines (42 loc) · 1.34 KB
/
SubscriptionStarted.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
<?php
namespace Thunk\Verbs\Examples\Subscriptions\Events;
use Thunk\Verbs\Event;
use Thunk\Verbs\Examples\Subscriptions\Models\Subscription;
use Thunk\Verbs\Examples\Subscriptions\States\GlobalReportState;
use Thunk\Verbs\Examples\Subscriptions\States\PlanReportState;
use Thunk\Verbs\Examples\Subscriptions\States\SubscriptionState;
use Thunk\Verbs\Support\StateCollection;
class SubscriptionStarted extends Event
{
public int $user_id;
public int $plan_id;
public ?int $subscription_id = null;
public function states(): StateCollection
{
$this->subscription_id ??= snowflake_id();
return new StateCollection([
SubscriptionState::load($this->subscription_id),
PlanReportState::load($this->plan_id),
GlobalReportState::singleton(),
]);
}
public function validate(SubscriptionState $state)
{
return ! $state->is_active;
}
public function apply(SubscriptionState $state)
{
$state->is_active = true;
$state->plan_id = $this->plan_id;
}
public function handle()
{
[$subscription_state] = $this->states();
Subscription::create([
'id' => $subscription_state->id,
'user_id' => $this->user_id,
'plan_id' => $this->plan_id,
'is_active' => true,
]);
}
}