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

DefaultResponseFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 34
rs 10
c 0
b 0
f 0
ccs 9
cts 10
cp 0.9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponse() 0 15 4
A getSupportedMIMETypes() 0 6 1
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