Response   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 1
dl 0
loc 63
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C json() 0 49 15
1
<?php
2
3
namespace FFMVC\Helpers;
4
5
/**
6
 * HTTP Response Helper Class.
7
 *
8
 * @author Vijay Mahrra <[email protected]>
9
 * @copyright (c) Copyright 2015 Vijay Mahrra
10
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
11
 */
12
class Response extends \Prefab
13
{
14
    /**
15
     * Encode the input parameter $data as JSON and output it with appropriate http headers.
16
     *
17
     * @param mixed $data   input variable, takes origin, age, methods
18
     * @param array $params parameters for the http headers: ttl, origin, methods (GET, POST, PUT, DELETE)
19
     *
20
     * @return void
21
     *
22
     * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
23
     * @see https://www.w3.org/TR/cors/
24
     */
25
    public static function json(array $data, array $params = [])
26
    {
27
        $f3 = \Base::instance();
28
29
        $body    = json_encode($data, JSON_PRETTY_PRINT);
30
        $headers = array_key_exists('headers', $params) ? $params['headers'] : [];
31
32
        // set ttl
33
        $ttl = (int) array_key_exists('ttl', $params) ? $params['ttl'] : 0; // cache for $ttl seconds
34
        if (empty($ttl)) {
35
            $ttl = 0;
36
        }
37
38
        $headers = array_merge($headers, [
39
            'Content-type'                     => 'application/json; charset=utf-8',
40
            'Expires'                          => Time::http(time() + $ttl),
41
            'Access-Control-Max-Age'           => $ttl,
42
            'Access-Control-Expose-Headers'    => array_key_exists('acl_expose_headers', $params) ? $params['acl_expose_headers'] : null,
43
            'Access-Control-Allow-Methods'     => array_key_exists('acl_http_methods', $params) ? $params['acl_http_methods'] : null,
44
            'Access-Control-Allow-Origin'      => array_key_exists('acl_origin', $params) ? $params['acl_origin'] : '*',
45
            'Access-Control-Allow-Credentials' => array_key_exists('acl_credentials', $params) && !empty($params['acl_credentials']) ?  'true' : 'false',
46
            'ETag'                             => array_key_exists('etag', $params) ? $params['etag'] : md5($body),
47
            'Content-Length'                   => \UTF::instance()->strlen($body),
48
        ]);
49
50
        // send the headers + data
51
        $f3->expire($ttl);
52
53
        // default status is 200 - OK
54
        $f3->status(array_key_exists('http_status', $params) ? $params['http_status'] : 200);
55
56
        // do not send session cookie
57
        if (!array_key_exists('cookie', $params)) {
58
            header_remove('Set-Cookie'); // prevent php session
59
        }
60
61
        ksort($headers);
62
        foreach ($headers as $header => $value) {
63
            if (!isset($value)) {
64
                continue;
65
            }
66
            header($header . ': ' . $value);
67
        }
68
69
        // HEAD request should be identical headers to GET request but no body
70
        if ('HEAD' !== $f3->get('VERB')) {
71
            echo $body;
72
        }
73
    }
74
}
75