Passed
Pull Request — master (#156)
by Matt
04:37
created

ApiController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 62
c 1
b 0
f 1
dl 0
loc 147
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A imagesIndex() 0 27 1
A wanderIndex() 0 44 1
A wandersShow() 0 37 1
1
<?php
2
3
namespace App\Controller\Api;
4
5
use App\Entity\Wander;
6
use App\Repository\ImageRepository;
7
use App\Repository\WanderRepository;
8
use App\Service\SettingsService;
9
use Knp\Component\Pager\Paginator;
10
use Knp\Component\Pager\PaginatorInterface;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Routing\Annotation\Route;
15
use Symfony\Component\Routing\RouterInterface;
16
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
17
18
/**
19
 * @Route("/api/", name="api_")
20
 */
21
class ApiController extends AbstractController
22
{
23
    /**
24
     *
25
     * API: Wander list. Returns a basic list of wanders.
26
     *
27
     * @Route(
28
     *  "wanders",
29
     *  name="wanders_index",
30
     *  methods={"GET"},
31
     *  format="json",
32
     *  condition="'application/json' in request.getAcceptableContentTypes()"
33
     * )
34
     */
35
    public function wanderIndex(
36
        WanderRepository $wanderRepository,
37
        RouterInterface $router
38
        ): Response
39
    {
40
        $wanders = $wanderRepository
41
            ->standardQueryBuilder()
42
            ->orderBy('w.startTime', 'asc')
43
            ->getQuery()
44
            ->execute();
45
46
        // It's nicer for our JavaScript to be handed the Wander URI on a plate, so we add it
47
        // to the returned JSON.
48
        $contentUrlCallback = function (
49
            /** @scrutinizer ignore-unused */ $innerObject,
50
            $outerObject,
51
            /** @scrutinizer ignore-unused */ string $attributeName,
52
            /** @scrutinizer ignore-unused */ string $format = null,
53
            /** @scrutinizer ignore-unused */ array $context = []
54
        ) use ($router) {
55
            return $router->generate(
56
                'wanders_show',
57
                ['id' => $outerObject->getId()]
58
            );
59
        };
60
61
        $response = $this->json(
62
            $wanders,
63
            Response::HTTP_OK,
64
            [],
65
            [
66
                'groups' => 'wander:list',
67
                AbstractNormalizer::CALLBACKS => [
68
                    'contentUrl' => $contentUrlCallback,
69
                ],
70
            ]
71
        );
72
73
        $response
74
            ->setPublic()
75
            ->setSharedMaxAge(3600)
76
            ->setMaxAge(3600);
77
78
        return $response;
79
    }
80
81
    /**
82
     * @Route(
83
     *  "wanders/{id}",
84
     *  name="wanders_show",
85
     *  methods={"GET"},
86
     *  format="json",
87
     *  condition="'application/json' in request.getAcceptableContentTypes()"
88
     * )
89
     */
90
    public function wandersShow(
91
        Wander $wander,
92
        RouterInterface $router
93
    ): Response {
94
        // It's nicer for our JavaScript to be handed the Wander URI on a plate, so we add it
95
        // to the returned JSON.
96
        $contentUrlCallback = function (
97
            /** @scrutinizer ignore-unused */ $innerObject,
98
            $outerObject,
99
            /** @scrutinizer ignore-unused */ string $attributeName,
100
            /** @scrutinizer ignore-unused */ string $format = null,
101
            /** @scrutinizer ignore-unused */ array $context = []
102
        ) use ($router) {
103
            return $router->generate(
104
                'wanders_show',
105
                ['id' => $outerObject->getId()]
106
            );
107
        };
108
109
        $response = $this->json(
110
            $wander,
111
            Response::HTTP_OK,
112
            [],
113
            [
114
                'groups' => 'wander:item',
115
                AbstractNormalizer::CALLBACKS => [
116
                    'contentUrl' => $contentUrlCallback,
117
                ],
118
            ]
119
        );
120
121
        $response
122
            ->setPublic()
123
            ->setSharedMaxAge(3600)
124
            ->setMaxAge(3600);
125
126
        return $response;
127
    }
128
129
    /**
130
     *
131
     * API: Image list. Returns a basic list of images.
132
     *
133
     * @Route(
134
     *  "images",
135
     *  name="images_index",
136
     *  methods={"GET"},
137
     *  format="json",
138
     *  condition="'application/json' in request.getAcceptableContentTypes()"
139
     * )
140
     */
141
    public function imagesIndex(
142
        ImageRepository $imageRepository
143
    ): Response
144
    {
145
        $results = $imageRepository
146
            ->standardQueryBuilder()
147
            ->where('i.latlng IS NOT NULL')
148
            ->getQuery()
149
            ->execute();
150
151
        $response = $this->json(
152
            $results,
153
            Response::HTTP_OK,
154
            [],
155
            [
156
                'groups' => 'image:list',
157
            ]
158
        );
159
160
        $response
161
            ->setPublic()
162
            ->setSharedMaxAge(3600)
163
            // It's only an experiment, and it's slow to calculate, especially as it's nearly
164
            // a megabyte response on the live site. Keep it in shared cache for a *long* time.
165
            ->setMaxAge(43200);
166
167
        return $response;
168
    }
169
}
170