| Conditions | 5 |
| Paths | 9 |
| Total Lines | 51 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 21 | protected function getStats(): array |
||
| 22 | { |
||
| 23 | $analytics = NotificationSetting::getAnalytics(); |
||
| 24 | |||
| 25 | if (!($analytics['enabled'] ?? config('notifier.settings.analytics.enabled', true))) { |
||
| 26 | return [ |
||
| 27 | Stat::make('Analytics Disabled', '') |
||
| 28 | ->description('Enable analytics in settings to view engagement metrics') |
||
| 29 | ->color('gray'), |
||
| 30 | ]; |
||
| 31 | } |
||
| 32 | |||
| 33 | $totalSent = Notification::where('status', 'sent')->count(); |
||
| 34 | $totalOpened = Notification::whereNotNull('opened_at')->count(); |
||
| 35 | $totalClicked = Notification::whereNotNull('clicked_at')->count(); |
||
| 36 | |||
| 37 | $totalOpens = Notification::sum('opens_count'); |
||
| 38 | $totalClicks = Notification::sum('clicks_count'); |
||
| 39 | |||
| 40 | // Calculate engagement rates |
||
| 41 | $openRate = $totalSent > 0 ? round(($totalOpened / $totalSent) * 100, 1) : 0; |
||
| 42 | $clickRate = $totalSent > 0 ? round(($totalClicked / $totalSent) * 100, 1) : 0; |
||
| 43 | $clickThroughRate = $totalOpened > 0 ? round(($totalClicked / $totalOpened) * 100, 1) : 0; |
||
| 44 | |||
| 45 | return [ |
||
| 46 | Stat::make('Total Opens', number_format($totalOpens)) |
||
|
|
|||
| 47 | ->description($totalOpened . ' unique opens') |
||
| 48 | ->descriptionIcon('heroicon-m-eye') |
||
| 49 | ->color('info') |
||
| 50 | ->chart($this->getOpensChartData()), |
||
| 51 | |||
| 52 | Stat::make('Open Rate', $openRate . '%') |
||
| 53 | ->description($totalOpened . ' of ' . $totalSent . ' emails opened') |
||
| 54 | ->descriptionIcon('heroicon-m-arrow-trending-up') |
||
| 55 | ->color('success'), |
||
| 56 | |||
| 57 | Stat::make('Total Clicks', number_format($totalClicks)) |
||
| 58 | ->description($totalClicked . ' unique clicks') |
||
| 59 | ->descriptionIcon('heroicon-m-cursor-arrow-rays') |
||
| 60 | ->color('warning') |
||
| 61 | ->chart($this->getClicksChartData()), |
||
| 62 | |||
| 63 | Stat::make('Click Rate', $clickRate . '%') |
||
| 64 | ->description($totalClicked . ' of ' . $totalSent . ' emails clicked') |
||
| 65 | ->descriptionIcon('heroicon-m-arrow-trending-up') |
||
| 66 | ->color('warning'), |
||
| 67 | |||
| 68 | Stat::make('Click-Through Rate', $clickThroughRate . '%') |
||
| 69 | ->description('Clicks per open') |
||
| 70 | ->descriptionIcon('heroicon-m-arrow-path') |
||
| 71 | ->color('success'), |
||
| 72 | ]; |
||
| 100 |