Completed
Push — master ( 55cac1...2cd3d9 )
by Matt
15s queued 13s
created

ApiController::imagesIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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