Verbs

Global Report

1<?php ...
2 
3namespace Thunk\Verbs\Examples\Subscriptions\States;
4 
5use Illuminate\Support\Carbon;
6use Thunk\Verbs\Examples\Subscriptions\Events\SubscriptionCancelled;
7use Thunk\Verbs\Examples\Subscriptions\Events\SubscriptionStarted;
8use Thunk\Verbs\State;
9 
10class GlobalReportState extends State
11{
12 public int $total_subscriptions = 0;
13 
14 public int $subscribes_since_last_report = 0;
15 
16 public int $unsubscribes_since_last_report = 0;
17 
18 public Carbon $last_reported_at;
19 
20 public function summary(): string
21 {
22 $churn = $this->subscribes_since_last_report > 0
23 ? ($this->unsubscribes_since_last_report / $this->subscribes_since_last_report) * 100
24 : 0;
25 
26 return implode('; ', [
27 "{$this->subscribes_since_last_report} subscribe(s)",
28 "{$this->unsubscribes_since_last_report} unsubscribe(s)",
29 "{$churn}% churn",
30 ]);
31 }
32 
33 public function applySubscriptionStarted(SubscriptionStarted $e)
34 {
35 $this->total_subscriptions++;
36 $this->subscribes_since_last_report++;
37 }
38 
39 public function applySubscriptionCancelled(SubscriptionCancelled $e)
40 {
41 $this->total_subscriptions--;
42 $this->unsubscribes_since_last_report++;
43 }
44}