Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ResponseFormatterTrait
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 getJsonResponse
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 addCorsHeaders
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * VuFind Action Feature Trait - HTTP response formatting support methods
5 *
6 * PHP version 8
7 *
8 * Copyright (C) The National Library of Finland 2022.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Controller_Plugins
25 * @author   Ere Maijala <ere.maijala@helsinki.fi>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Page
28 */
29
30namespace VuFind\Controller\Feature;
31
32use Laminas\Http\Response;
33
34/**
35 * VuFind Action Feature Trait - HTTP response formatting support methods
36 *
37 * @category VuFind
38 * @package  Controller_Plugins
39 * @author   Ere Maijala <ere.maijala@helsinki.fi>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org Main Page
42 */
43trait ResponseFormatterTrait
44{
45    /**
46     * Get a JSON response from an array of data
47     *
48     * @param array $data       Data to encode
49     * @param int   $statusCode HTTP status code
50     *
51     * @return Response
52     */
53    protected function getJsonResponse(array $data, int $statusCode = 200): Response
54    {
55        $response = new Response();
56        $response->setStatusCode($statusCode);
57        $response->getHeaders()->addHeaderLine('Content-type', 'application/json');
58        $response->setContent(json_encode($data));
59        return $response;
60    }
61
62    /**
63     * Add CORS headers to a response.
64     *
65     * @param Response $response         Response
66     * @param array    $allowedMethods   Allowed HTTP methods
67     * @param array    $allowedHeaders   Allowed HTTP headers
68     * @param string   $allowedOrigin    Allowed origin (see
69     * https://developer.mozilla.org/
70     * en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin for details)
71     * @param bool     $allowCredentials Whether credentials are allowed
72     * @param int      $maxAge           Maximum time in seconds the information
73     * from a preflight request can be cached
74     *
75     * @return void
76     */
77    protected function addCorsHeaders(
78        Response $response,
79        array $allowedMethods = ['GET', 'POST', 'OPTIONS'],
80        array $allowedHeaders = [],
81        string $allowedOrigin = '*',
82        bool $allowCredentials = false,
83        int $maxAge = 86400
84    ): void {
85        $headers = $response->getHeaders();
86        $headers->addHeaderLine(
87            'Access-Control-Allow-Methods',
88            implode(', ', $allowedMethods)
89        );
90        if ($allowedHeaders) {
91            $headers->addHeaderLine(
92                'Access-Control-Allow-Headers',
93                implode(', ', $allowedHeaders)
94            );
95        }
96        $headers->addHeaderLine(
97            "Access-Control-Allow-Origin: $allowedOrigin"
98        );
99        if ('*' !== $allowedOrigin) {
100            $headers->addHeaderLine('Vary: Origin');
101        }
102        if ($allowCredentials) {
103            // Note: true is the only valid value; false must not be used.
104            $headers->addHeaderLine(
105                'Access-Control-Allow-Credentials: true'
106            );
107        }
108        $headers->addHeaderLine('Access-Control-Max-Age', $maxAge);
109    }
110}