Completed
Push — master ( f9ded0...b13d5b )
by John
02:58 queued 01:00
created

DefaultResponseFactory::getResponse()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
ccs 7
cts 8
cp 0.875
cc 4
eloc 8
nc 6
nop 2
crap 4.0312
1
<?php
2
namespace LunixREST\APIResponse;
3
4
use LunixREST\APIResponse\Exceptions\NotAcceptableResponseTypeException;
5
6
/**
7
 * Class DefaultResponseFactory
8
 * @package LunixREST\APIResponse
9
 */
10
class DefaultResponseFactory implements ResponseFactory
11
{
12
    /**
13
     * @param null|object|array $data
14
     * @param array $acceptedMIMETypes
15
     * @return APIResponse
16
     * @throws NotAcceptableResponseTypeException
17
     */
18 4
    public function getResponse($data, array $acceptedMIMETypes): APIResponse
19
    {
20 4
        if (empty($acceptedMIMETypes)) {
21
            $acceptedMIMETypes = $this->getSupportedMIMETypes();
22
        }
23
24 4
        foreach ($acceptedMIMETypes as $acceptedMIMEType) {
25 4
            switch (strtolower($acceptedMIMEType)) {
26 4
                case "application/json":
27 4
                    return new JSONResponse($data);
28
            }
29
        }
30
31 1
        throw new NotAcceptableResponseTypeException("None of the accepted types are supported");
32
    }
33
34
    /**
35
     * @return string[]
36
     */
37 1
    public function getSupportedMIMETypes(): array
38
    {
39
        return [
40
            "application/json"
41 1
        ];
42
    }
43
}
44