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

ApiController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A imagesIndex() 0 27 1
A wanderIndex() 0 40 1
A wandersShow() 0 32 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
        Request $request,
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

36
        /** @scrutinizer ignore-unused */ Request $request,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
        WanderRepository $wanderRepository,
38
        PaginatorInterface $paginator,
0 ignored issues
show
Unused Code introduced by
The parameter $paginator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

38
        /** @scrutinizer ignore-unused */ PaginatorInterface $paginator,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
        RouterInterface $router
40
        ): Response
41
    {
42
        $wanders = $wanderRepository
43
            ->standardQueryBuilder()
44
            ->orderBy('w.startTime', 'asc')
45
            ->getQuery()
46
            ->execute();
47
48
        // It's nicer for our JavaScript to be handed the Wander URI on a plate, so we add it
49
        // to the returned JSON.
50
        $contentUrlCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) use ($router) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

50
        $contentUrlCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, /** @scrutinizer ignore-unused */ array $context = []) use ($router) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attributeName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

50
        $contentUrlCallback = function ($innerObject, $outerObject, /** @scrutinizer ignore-unused */ string $attributeName, string $format = null, array $context = []) use ($router) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

50
        $contentUrlCallback = function ($innerObject, $outerObject, string $attributeName, /** @scrutinizer ignore-unused */ string $format = null, array $context = []) use ($router) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
            return $router->generate(
52
                'wanders_show',
53
                ['id' => $outerObject->getId()]
54
            );
55
        };
56
57
        $response = $this->json(
58
            $wanders,
59
            Response::HTTP_OK,
60
            [],
61
            [
62
                'groups' => 'wander:list',
63
                AbstractNormalizer::CALLBACKS => [
64
                    'contentUrl' => $contentUrlCallback,
65
                ],
66
            ]
67
        );
68
69
        $response
70
            ->setPublic()
71
            ->setSharedMaxAge(3600)
72
            ->setMaxAge(3600);
73
74
        return $response;
75
    }
76
77
    /**
78
     * @Route(
79
     *  "wanders/{id}",
80
     *  name="wanders_show",
81
     *  methods={"GET"},
82
     *  format="json",
83
     *  condition="'application/json' in request.getAcceptableContentTypes()"
84
     * )
85
     */
86
    public function wandersShow(
87
        Request $request,
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

87
        /** @scrutinizer ignore-unused */ Request $request,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
        Wander $wander,
89
        RouterInterface $router
90
    ): Response {
91
        // It's nicer for our JavaScript to be handed the Wander URI on a plate, so we add it
92
        // to the returned JSON.
93
        $contentUrlCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) use ($router) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

93
        $contentUrlCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, /** @scrutinizer ignore-unused */ array $context = []) use ($router) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

93
        $contentUrlCallback = function ($innerObject, $outerObject, string $attributeName, /** @scrutinizer ignore-unused */ string $format = null, array $context = []) use ($router) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attributeName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

93
        $contentUrlCallback = function ($innerObject, $outerObject, /** @scrutinizer ignore-unused */ string $attributeName, string $format = null, array $context = []) use ($router) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
            return $router->generate(
95
                'wanders_show',
96
                ['id' => $outerObject->getId()]
97
            );
98
        };
99
100
        $response = $this->json(
101
            $wander,
102
            Response::HTTP_OK,
103
            [],
104
            [
105
                'groups' => 'wander:item',
106
                AbstractNormalizer::CALLBACKS => [
107
                    'contentUrl' => $contentUrlCallback,
108
                ],
109
            ]
110
        );
111
112
        $response
113
            ->setPublic()
114
            ->setSharedMaxAge(3600)
115
            ->setMaxAge(3600);
116
117
        return $response;
118
    }
119
120
    /**
121
     *
122
     * API: Image list. Returns a basic list of images.
123
     *
124
     * @Route(
125
     *  "images",
126
     *  name="images_index",
127
     *  methods={"GET"},
128
     *  format="json",
129
     *  condition="'application/json' in request.getAcceptableContentTypes()"
130
     * )
131
     */
132
    public function imagesIndex(
133
        ImageRepository $imageRepository
134
    ): Response
135
    {
136
        $results = $imageRepository
137
            ->standardQueryBuilder()
138
            ->where('i.latlng IS NOT NULL')
139
            ->getQuery()
140
            ->execute();
141
142
        $response = $this->json(
143
            $results,
144
            Response::HTTP_OK,
145
            [],
146
            [
147
                'groups' => 'image:list',
148
            ]
149
        );
150
151
        $response
152
            ->setPublic()
153
            ->setSharedMaxAge(3600)
154
            // It's only an experiment, and it's slow to calculate, especially as it's nearly
155
            // a megabyte response on the live site. Keep it in shared cache for a *long* time.
156
            ->setMaxAge(43200);
157
158
        return $response;
159
    }
160
}
161