Verbs

Subscription Cancelled

1<?php ...
2 
3namespace Thunk\Verbs\Examples\Subscriptions\Events;
4 
5use Thunk\Verbs\Attributes\Autodiscovery\AppliesToChildState;
6use Thunk\Verbs\Attributes\Autodiscovery\AppliesToSingletonState;
7use Thunk\Verbs\Attributes\Autodiscovery\AppliesToState;
8use Thunk\Verbs\Event;
9use Thunk\Verbs\Examples\Subscriptions\Models\Subscription;
10use Thunk\Verbs\Examples\Subscriptions\States\GlobalReportState;
11use Thunk\Verbs\Examples\Subscriptions\States\PlanReportState;
12use Thunk\Verbs\Examples\Subscriptions\States\SubscriptionState;
13 
14#[AppliesToState(state_type: SubscriptionState::class, id: 'subscription_id', alias: 'subscription')]
15#[AppliesToChildState(state_type: PlanReportState::class, parent_type: SubscriptionState::class, id: 'plan_id', alias: 'plan')]
16#[AppliesToSingletonState(state_type: GlobalReportState::class, alias: 'report')]
17class SubscriptionCancelled extends Event
18{
19 public int $subscription_id;
20 
21 public function validate(SubscriptionState $state)
22 {
23 return $state->is_active;
24 }
25 
26 public function apply(SubscriptionState $state)
27 {
28 $state->is_active = false;
29 }
30 
31 public function handle()
32 {
33 $subscription = Subscription::find($this->subscription_id);
34 
35 $subscription->is_active = false;
36 $subscription->cancelled_at = now();
37 $subscription->save();
38 }
39}