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