Responder::getResponse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 21
rs 9.3142
cc 3
eloc 15
nc 3
nop 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(
22
            $headers,
23
            $content,
24
            $response
25
        );
26
    }
27
28
    /**
29
     * @param array $headers
30
     * @param $content
31
     * @param $response
32
     * @return mixed
33
     */
34
    public static function getResponse($headers = [], $content, $response)
35
    {
36
        $body = $response->getBody();
37
        $body->write($content);
38
        $i = 0;
39
        foreach ($headers as $header => $value) {
40
            if ($i === 0) {
41
                $response = $response->withHeader(
42
                    $header,
43
                    $value
44
                );
45
            } else {
46
                $response = $response->withAddedHeader(
47
                    $header,
48
                    $value
49
                );
50
            }
51
            $i++;
52
        }
53
        return $response->withBody($body);
54
    }
55
56
}