Passed
Pull Request — master (#156)
by Matt
06:17
created

ApiController::wanderIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 21
c 2
b 0
f 1
nc 1
nop 2
dl 0
loc 34
rs 9.584
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
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
18
19
/**
20
 * @Route("/api/", name="api_")
21
 */
22
class ApiController extends AbstractController
23
{
24
    /**
25
     *
26
     * API: Wander list. Returns a basic list of wanders.
27
     *
28
     * @Route(
29
     *  "wanders",
30
     *  name="wanders_index",
31
     *  methods={"GET"},
32
     *  format="json",
33
     *  condition="'application/json' in request.getAcceptableContentTypes()"
34
     * )
35
     * @Cache(
36
     *  public="true",
37
     *  smaxage="3600",
38
     *  maxage="3600"
39
     * )
40
     */
41
    public function wanderIndex(
42
        WanderRepository $wanderRepository,
43
        RouterInterface $router
44
        ): Response
45
    {
46
        $wanders = $wanderRepository
47
            ->standardQueryBuilder()
48
            ->orderBy('w.startTime', 'asc')
49
            ->getQuery()
50
            ->execute();
51
52
        // It's nicer for our JavaScript to be handed the Wander URI on a plate, so we add it
53
        // to the returned JSON.
54
        $contentUrlCallback = function (
55
            /** @scrutinizer ignore-unused */ $innerObject,
56
            $outerObject,
57
            /** @scrutinizer ignore-unused */ string $attributeName,
58
            /** @scrutinizer ignore-unused */ string $format = null,
59
            /** @scrutinizer ignore-unused */ array $context = []
60
        ) use ($router) {
61
            return $router->generate(
62
                'wanders_show',
63
                ['id' => $outerObject->getId()]
64
            );
65
        };
66
67
        return $this->json(
68
            $wanders,
69
            Response::HTTP_OK,
70
            [],
71
            [
72
                'groups' => 'wander:list',
73
                AbstractNormalizer::CALLBACKS => [
74
                    'contentUrl' => $contentUrlCallback,
75
                ],
76
            ]
77
        );
78
    }
79
80
    /**
81
     * @Route(
82
     *  "wanders/{id}",
83
     *  name="wanders_show",
84
     *  methods={"GET"},
85
     *  format="json",
86
     *  condition="'application/json' in request.getAcceptableContentTypes()"
87
     * )
88
     * @Cache(
89
     *  public="true",
90
     *  smaxage="3600",
91
     *  maxage="3600"
92
     * )
93
     */
94
    public function wandersShow(
95
        Wander $wander,
96
        RouterInterface $router
97
    ): Response {
98
        // It's nicer for our JavaScript to be handed the Wander URI on a plate, so we add it
99
        // to the returned JSON.
100
        $contentUrlCallback = function (
101
            /** @scrutinizer ignore-unused */ $innerObject,
102
            $outerObject,
103
            /** @scrutinizer ignore-unused */ string $attributeName,
104
            /** @scrutinizer ignore-unused */ string $format = null,
105
            /** @scrutinizer ignore-unused */ array $context = []
106
        ) use ($router) {
107
            return $router->generate(
108
                'wanders_show',
109
                ['id' => $outerObject->getId()]
110
            );
111
        };
112
113
        return $this->json(
114
            $wander,
115
            Response::HTTP_OK,
116
            [],
117
            [
118
                'groups' => 'wander:item',
119
                AbstractNormalizer::CALLBACKS => [
120
                    'contentUrl' => $contentUrlCallback,
121
                ],
122
            ]
123
        );
124
    }
125
126
    /**
127
     *
128
     * API: Image list. Returns a basic list of images.
129
     *
130
     * @Route(
131
     *  "images",
132
     *  name="images_index",
133
     *  methods={"GET"},
134
     *  format="json",
135
     *  condition="'application/json' in request.getAcceptableContentTypes()"
136
     * )
137
     * @Cache(
138
     *  public="true",
139
     *  smaxage="3600",
140
     *  maxage="43200"
141
     * )
142
     */
143
    public function imagesIndex(
144
        ImageRepository $imageRepository
145
    ): Response
146
    {
147
        $results = $imageRepository
148
            ->standardQueryBuilder()
149
            ->where('i.latlng IS NOT NULL')
150
            ->getQuery()
151
            ->execute();
152
153
        return $this->json(
154
            $results,
155
            Response::HTTP_OK,
156
            [],
157
            [
158
                'groups' => 'image:list',
159
            ]
160
        );
161
    }
162
}
163