Passed
Pull Request — master (#147)
by Matt
04:32
created

StatsController::buildPeriodicChart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 30
rs 9.7
1
<?php
2
3
namespace App\Controller;
4
5
use App\Service\StatsService;
6
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\Routing\Annotation\Route;
9
use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
10
use Symfony\UX\Chartjs\Model\Chart;
11
12
class StatsController extends AbstractController
13
{
14
    /**
15
     * @Route("/stats", name="stats_index")
16
     */
17
    public function index(
18
        StatsService $statsService,
19
        ChartBuilderInterface $chartBuilder
20
    ): Response
21
    {
22
        $wanderStats = $statsService->getWanderStats();
23
        $imageStats = $statsService->getImageStats();
24
25
        $monthlyChart = $this->buildPeriodicChart($wanderStats['monthlyStats'], $chartBuilder);
26
        $yearlyChart = $this->buildPeriodicChart($wanderStats['yearlyStats'], $chartBuilder);
27
28
        return $this->render('stats/index.html.twig', [
29
            'controller_name' => 'StatsController', // TODO: Remove this boilerplate
30
            'imageStats' => $imageStats,
31
            'wanderStats' => $wanderStats,
32
            'monthlyChart' => $monthlyChart,
33
            'yearlyChart' => $yearlyChart
34
        ]);
35
    }
36
    /**
37
     * Builds the data for a periodic Chartjs stats chart, e.g. wanders/distance/etc per month or year.
38
     *
39
     * @return Chart
40
     * @param array<string, mixed> $periodicStats
41
     * @param ChartBuilderInterface $chartBuilder
42
     */
43
    private function buildPeriodicChart(
44
        array $periodicStats,
45
        ChartBuilderInterface $chartBuilder
46
    ): Chart {
47
        $chart = $chartBuilder->createChart(Chart::TYPE_BAR);
48
        $chart->setData([
49
            'labels' => array_map(fn($dp): string => $dp['periodLabel'], $periodicStats),
50
            'datasets' => [
51
                // TODO: These colours should be defined in CSS. Add class somehow?
52
                [
53
                    'label' => 'Number of Wanders',
54
                    'backgroundColor' => '#ff66b2',
55
                    'borderColor' => 'black',
56
                    'data' => array_map(fn($dp): int => $dp['numberOfWanders'], $periodicStats),
57
                ],
58
                [
59
                    'label' => 'Distance Walked (km)',
60
                    'backgroundColor' => '#66ffff',
61
                    'borderColor' => 'black',
62
                    'data' => array_map(fn($dp): string => number_format($dp['totalDistance'] / 1000.0, 2), $periodicStats),
63
                ],
64
                [
65
                    'label' => 'Photos Taken',
66
                    'backgroundColor' => '#ffb266',
67
                    'borderColor' => 'black',
68
                    'data' => array_map(fn($dp): int => $dp['numberOfImages'], $periodicStats),
69
                ]
70
            ]
71
        ]);
72
        return $chart;
73
    }
74
}
75
76