Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
CatalogController
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 catalog
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 import
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5use Psr\Http\Message\ResponseInterface as Response;
6use Psr\Http\Message\ServerRequestInterface as Request;
7use UppServices\CatalogService;
8
9/**
10 * Slim controller for catalog endpoints.
11 * Returns the default product catalog (merged JSON files).
12 */
13class CatalogController
14{
15    private readonly CatalogService $catalogService;
16
17    public function __construct(CatalogService $catalogService)
18    {
19        $this->catalogService = $catalogService;
20    }
21
22    /**
23     * GET /catalog/catalog - returns the merged product catalog from static JSON files.
24     */
25    public function catalog(Request $request, Response $response): Response
26    {
27        $data = $this->catalogService->load();
28        $response->getBody()->write(json_encode($data));
29        return $response->withHeader('Content-Type', 'application/json');
30    }
31
32    /**
33     * POST /catalog/import - import catalog (zip with catalog.json) into place DB.
34     * Query params: device, session, place. Body: JSON with transfer data.
35     */
36    public function import(Request $request, Response $response): Response
37    {
38        $queryParams = $request->getQueryParams();
39        $postBody = $request->getBody()->getContents();
40        $result = $this->catalogService->import($queryParams, $postBody ?: null);
41        $response->getBody()->write(json_encode($result));
42        return $response->withHeader('Content-Type', 'application/json');
43    }
44}