|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Livewire\Customer; |
| 4 | + |
| 5 | +use App\Enums\Subscription; |
| 6 | +use Livewire\Attributes\Computed; |
| 7 | +use Livewire\Attributes\Layout; |
| 8 | +use Livewire\Attributes\Title; |
| 9 | +use Livewire\Component; |
| 10 | + |
| 11 | +#[Layout('components.layouts.dashboard')] |
| 12 | +#[Title('Dashboard')] |
| 13 | +class Dashboard extends Component |
| 14 | +{ |
| 15 | + #[Computed] |
| 16 | + public function licenseCount(): int |
| 17 | + { |
| 18 | + return auth()->user()->licenses()->count(); |
| 19 | + } |
| 20 | + |
| 21 | + #[Computed] |
| 22 | + public function isEapCustomer(): bool |
| 23 | + { |
| 24 | + return auth()->user()->isEapCustomer(); |
| 25 | + } |
| 26 | + |
| 27 | + #[Computed] |
| 28 | + public function activeSubscription() |
| 29 | + { |
| 30 | + return auth()->user()->subscription(); |
| 31 | + } |
| 32 | + |
| 33 | + #[Computed] |
| 34 | + public function subscriptionName(): ?string |
| 35 | + { |
| 36 | + $subscription = $this->activeSubscription; |
| 37 | + |
| 38 | + if (! $subscription) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + if ($subscription->stripe_price) { |
| 43 | + try { |
| 44 | + return Subscription::fromStripePriceId($subscription->stripe_price)->name(); |
| 45 | + } catch (\RuntimeException) { |
| 46 | + return ucfirst($subscription->type); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return ucfirst($subscription->type); |
| 51 | + } |
| 52 | + |
| 53 | + #[Computed] |
| 54 | + public function pluginLicenseCount(): int |
| 55 | + { |
| 56 | + return auth()->user()->pluginLicenses()->count(); |
| 57 | + } |
| 58 | + |
| 59 | + #[Computed] |
| 60 | + public function renewalLicenseKey(): ?string |
| 61 | + { |
| 62 | + if ($this->activeSubscription) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + $highestTierLicense = auth()->user()->licenses() |
| 67 | + ->whereIn('policy_name', ['max', 'pro', 'mini']) |
| 68 | + ->orderByRaw("CASE policy_name WHEN 'max' THEN 1 WHEN 'pro' THEN 2 WHEN 'mini' THEN 3 END") |
| 69 | + ->first(); |
| 70 | + |
| 71 | + return $highestTierLicense?->key; |
| 72 | + } |
| 73 | + |
| 74 | + #[Computed] |
| 75 | + public function connectedAccountsCount(): int |
| 76 | + { |
| 77 | + $user = auth()->user(); |
| 78 | + |
| 79 | + return ($user->hasGitHubToken() ? 1 : 0) + ($user->hasDiscordConnected() ? 1 : 0); |
| 80 | + } |
| 81 | + |
| 82 | + #[Computed] |
| 83 | + public function connectedAccountsDescription(): string |
| 84 | + { |
| 85 | + $user = auth()->user(); |
| 86 | + $hasGitHub = $user->hasGitHubToken(); |
| 87 | + $hasDiscord = $user->hasDiscordConnected(); |
| 88 | + |
| 89 | + return match (true) { |
| 90 | + $hasGitHub && $hasDiscord => 'GitHub & Discord', |
| 91 | + $hasGitHub => 'GitHub connected', |
| 92 | + $hasDiscord => 'Discord connected', |
| 93 | + default => 'No accounts connected', |
| 94 | + }; |
| 95 | + } |
| 96 | + |
| 97 | + #[Computed] |
| 98 | + public function totalPurchases(): int |
| 99 | + { |
| 100 | + $user = auth()->user(); |
| 101 | + |
| 102 | + return $this->licenseCount |
| 103 | + + $this->pluginLicenseCount |
| 104 | + + $user->productLicenses()->count(); |
| 105 | + } |
| 106 | +} |
0 commit comments