Completed
Push — master ( 84309c...24e320 )
by Vincenzo
02:46
created

Responder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getJsonResponse() 0 5 1
A getResponse() 0 21 3
1
<?php
2
3
namespace App\Lib\Helpers;
4
5
6
/**
7
 * Class Responder
8
 * @package App\Lib\Helpers
9
 */
10
class Responder
11
{
12
    /**
13
     * @param $content
14
     * @param $response
15
     * @param array $headers
16
     * @return mixed
17
     */
18
    public static function getJsonResponse($content, $response, $headers = [])
19
    {
20
        $headers['Content-Type'] = 'application/json';
21
        return self::getResponse($headers, $content, $response);
22
    }
23
24
    /**
25
     * @param array $headers
26
     * @param $content
27
     * @param $response
28
     * @return mixed
29
     */
30
    public static function getResponse($headers = [], $content, $response)
31
    {
32
        $body = $response->getBody();
33
        $body->write($content);
34
        $i = 0;
35
        foreach ($headers as $header => $value) {
36
            if ($i === 0) {
37
                $response = $response->withHeader(
38
                    $header,
39
                    $value
40
                );
41
            } else {
42
                $response = $response->withAddedHeader(
43
                    $header,
44
                    $value
45
                );
46
            }
47
            $i++;
48
        }
49
        return $response->withBody($body);
50
    }
51
52
}