Completed
Push — master ( 5cc45f...cc9f19 )
by Pavel
14s
created

ApiRenderer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 2
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A jsonResponse() 0 14 1
1
<?php
2
namespace App\Common;
3
4
use Psr\Http\Message\ResponseInterface as Response;
5
6
final class ApiRenderer
7
{
8
    /**
9
     * @var
10
     */
11
    private $config;
12
13
    /**
14
     * Renderer constructor.
15
     *
16
     * @param $config
17
     */
18
    public function __construct($config)
19
    {
20
        $this->config = $config;
21
    }
22
23
    /**
24
     * @param Response $response
25
     * @param int      $statusCode
26
     * @param string   $data
27
     *
28
     * @return Response
29
     */
30
    public function jsonResponse(Response $response, $statusCode = 200, $data = '')
31
    {
32
        $jsonResponse = $response
33
            ->withHeader('Access-Control-Allow-Origin', '*')
34
            ->withHeader('Access-Control-Allow-Methods', 'GET, PUT, PATCH, POST, DELETE, OPTIONS')
35
            ->withHeader('Access-Control-Allow-Credentials', 'true')
36
            ->withHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With')
37
            ->withHeader('Content-Type', 'application/vnd.api+json')
38
            ->withStatus($statusCode);
39
40
        $jsonResponse->getBody()->write($data);
41
42
        return $jsonResponse;
43
    }
44
}
45