Passed
Push — master ( cce8e8...044bfa )
by Matt
04:24
created

StatsController::index()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 103
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 66
c 4
b 0
f 0
nc 1
nop 2
dl 0
loc 103
rs 8.7418

How to fix   Long Method   

Long Method

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:

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
        $wanderStats = $statsService->getWanderStats();
22
        $imageStats = $statsService->getImageStats();
23
24
        $wanderNumberColour = '#2491B3';
25
        $wanderDistanceColour = '#ffb266';
26
27
        $imagesNumberColour = '#BF5439';
28
29
        $barBorderRadius = 5;
30
31
        $monthlyWanderChart = $chartBuilder->createChart(Chart::TYPE_BAR);
32
        $monthlyWanderChart->setData([
33
            'labels' => array_map(fn($dp): string => $dp['periodLabel'], $wanderStats['monthlyStats']),
34
            'datasets' => [
35
                // TODO: These colours should be defined in CSS. Add class somehow?
36
                [
37
                    'label' => 'Number of Wanders',
38
                    'backgroundColor' => $wanderNumberColour,
39
                    'borderColor' => 'black',
40
                    'borderWidth' => 1,
41
                    'borderRadius' => $barBorderRadius,
42
                    'data' => array_map(fn($dp): int => $dp['numberOfWanders'], $wanderStats['monthlyStats']),
43
44
                ],
45
                [
46
                    'label' => 'Distance Walked (km)',
47
                    'backgroundColor' => $wanderDistanceColour,
48
                    'borderColor' => 'black',
49
                    'borderWidth' => 1,
50
                    'borderRadius' => $barBorderRadius,
51
                    'data' => array_map(fn($dp): string => number_format($dp['totalDistance'] / 1000.0, 2), $wanderStats['monthlyStats']),
52
                ]
53
            ]
54
        ]);
55
56
        $yearlyWanderChart = $chartBuilder->createChart(Chart::TYPE_BAR);
57
        $yearlyWanderChart->setData([
58
            'labels' => array_map(fn($dp): string => $dp['periodLabel'], $wanderStats['yearlyStats']),
59
            'datasets' => [
60
                // TODO: These colours should be defined in CSS. Add class somehow?
61
                [
62
                    'label' => 'Number of Wanders',
63
                    'backgroundColor' => $wanderNumberColour,
64
                    'borderColor' => 'black',
65
                    'borderWidth' => 1,
66
                    'borderRadius' => $barBorderRadius,
67
                    'data' => array_map(fn($dp): int => $dp['numberOfWanders'], $wanderStats['yearlyStats']),
68
                ],
69
                [
70
                    'label' => 'Distance Walked (km)',
71
                    'backgroundColor' => $wanderDistanceColour,
72
                    'borderColor' => 'black',
73
                    'borderWidth' => 1,
74
                    'borderRadius' => $barBorderRadius,
75
                    'data' => array_map(fn($dp): string => number_format($dp['totalDistance'] / 1000.0, 2), $wanderStats['yearlyStats']),
76
                ]
77
            ]
78
        ]);
79
80
        $monthlyImagesChart = $chartBuilder->createChart(Chart::TYPE_BAR);
81
        $monthlyImagesChart->setData([
82
            'labels' => array_map(fn($dp): string => $dp['periodLabel'], $wanderStats['monthlyStats']),
83
            'datasets' => [
84
                // TODO: These colours should be defined in CSS. Add class somehow?
85
                [
86
                    'label' => 'Photos Taken',
87
                    'backgroundColor' => $imagesNumberColour, // '#ffb266',
88
                    'borderColor' => 'black',
89
                    'borderWidth' => 1,
90
                    'borderRadius' => $barBorderRadius,
91
                    'data' => array_map(fn($dp): int => $dp['numberOfImages'], $wanderStats['monthlyStats']),
92
                ]
93
            ]
94
        ]);
95
96
        $yearlyImagesChart = $chartBuilder->createChart(Chart::TYPE_BAR);
97
        $yearlyImagesChart->setData([
98
            'labels' => array_map(fn($dp): string => $dp['periodLabel'], $wanderStats['yearlyStats']),
99
            'datasets' => [
100
                // TODO: These colours should be defined in CSS. Add class somehow?
101
                [
102
                    'label' => 'Photos Taken',
103
                    'backgroundColor' => $imagesNumberColour, // '#ffb266',
104
                    'borderColor' => 'black',
105
                    'borderWidth' => 1,
106
                    'borderRadius' => $barBorderRadius,
107
                    'data' => array_map(fn($dp): int => $dp['numberOfImages'], $wanderStats['yearlyStats']),
108
                ]
109
            ]
110
        ]);
111
112
        return $this->render('stats/index.html.twig', [
113
            'controller_name' => 'StatsController', // TODO: Remove this boilerplate
114
            'imageStats' => $imageStats,
115
            'wanderStats' => $wanderStats,
116
            'monthlyWanderChart' => $monthlyWanderChart,
117
            'yearlyWanderChart' => $yearlyWanderChart,
118
            'monthlyImagesChart' => $monthlyImagesChart,
119
            'yearlyImagesChart' => $yearlyImagesChart
120
        ]);
121
    }
122
}
123