Completed
Push — master ( 50fe31...12fa86 )
by oxidmod
17s
created

ContentTypeModifier::modify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
3
declare (strict_types=1);
4
5
namespace Oxidmod\RestBundle\Response\Modifier;
6
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
10
/**
11
 * Add application/json header
12
 */
13
class ContentTypeModifier implements ResponseModifierInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $contentType;
19
20
    /**
21
     * @param string $contentType
22
     */
23
    public function __construct(string $contentType)
24
    {
25
        $this->contentType = $contentType;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function modify(Response $response, Request $request): Response
32
    {
33
        $acceptableTypes = $request->getAcceptableContentTypes();
34
35
        foreach ($acceptableTypes as $acceptableType) {
36
            if ($acceptableType === $this->contentType) {
37
                $clone = clone $response;
38
                $clone->headers->set('Content-Type', $acceptableType);
39
40
                return $clone;
41
            }
42
        }
43
44
        return $response;
45
    }
46
}
47