-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDashboardController.php
More file actions
executable file
·89 lines (79 loc) · 3.04 KB
/
Copy pathDashboardController.php
File metadata and controls
executable file
·89 lines (79 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
declare(strict_types=1);
namespace PhpList\WebFrontend\Controller;
use PhpList\RestApiClient\Exception\AuthorizationException;
use PhpList\RestApiClient\Endpoint\StatisticsClient;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class DashboardController extends AbstractController
{
public function __construct(private readonly StatisticsClient $statisticsClient)
{
}
#[Route('/', name: 'home', methods: ['GET'])]
public function index(Request $request): Response
{
$dashboardStats = [];
$dashboardError = null;
try {
$stats = $this->statisticsClient->getDashboardStats();
$dashboardStats = $this->buildDashboardStats($stats);
} catch (AuthorizationException $e) {
$dashboardError = $e->getMessage() ?: 'Unable to load dashboard statistics.';
}
return $this->render('@PhpListFrontend/spa.html.twig', [
'page' => 'Dashboard',
'dashboard_stats' => $dashboardStats,
'dashboard_error' => $dashboardError,
]);
}
private function buildDashboardStats(object $stats): array
{
$recentCampaigns = [];
foreach ($stats->recentCampaigns as $campaign) {
$recentCampaigns[] = [
'name' => $campaign->name,
'status' => $campaign->status,
'date' => $campaign->date?->format('Y-m-d') ?? '',
'openRate' => $campaign->openRate,
'clickRate' => $campaign->clickRate,
];
}
$chartLabels = [];
$chartOpens = [];
$chartClicks = [];
foreach ($stats->campaignPerformance as $point) {
$chartLabels[] = $point->date?->format('M d') ?? '';
$chartOpens[] = $point->opens;
$chartClicks[] = $point->clicks;
}
return [
'total_subscribers' => [
'value' => $stats->totalSubscribers->value,
'change_vs_last_month' => $stats->totalSubscribers->changeVsLastMonth,
],
'active_campaigns' => [
'value' => $stats->activeCampaigns->value,
'change_vs_last_month' => $stats->activeCampaigns->changeVsLastMonth,
],
'open_rate' => [
'value' => $stats->openRate->value,
'change_vs_last_month' => $stats->openRate->changeVsLastMonth,
],
'bounce_rate' => [
'value' => $stats->bounceRate->value,
'change_vs_last_month' => $stats->bounceRate->changeVsLastMonth,
],
'recent_campaigns' => $recentCampaigns,
'chart' => [
'labels' => $chartLabels,
'series' => [
['name' => 'Opens', 'data' => $chartOpens],
['name' => 'Clicks', 'data' => $chartClicks],
],
],
];
}
}