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

StatsController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 86
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 52
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 86
rs 9.0472

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