Passed
Pull Request — master (#72)
by Dmitriy
29:29 queued 14:31
created

JsonDataConverter::convertData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\StreamFactoryInterface;
9
use Psr\Http\Message\StreamInterface;
10
use Yiisoft\Serializer\JsonSerializer;
11
12
final class JsonDataConverter implements DataConverterInterface
13
{
14
    /**
15
     * @var string the Content-Type header for the response
16
     */
17
    private string $contentType = 'application/json';
18
19
    private JsonSerializer $jsonSerializer;
20
21
    private StreamFactoryInterface $streamFactory;
22
23
    public function __construct(
24
        JsonSerializer $jsonSerializer,
25
        StreamFactoryInterface $streamFactory
26
    ) {
27
        $this->jsonSerializer = $jsonSerializer;
28
        $this->streamFactory = $streamFactory;
29
    }
30
31
    public function convertData($data, ResponseInterface &$response): StreamInterface
32
    {
33
        $content = $this->jsonSerializer->serialize($data);
34
        $response = $response->withHeader('Content-Type', $this->contentType);
35
36
        return $this->streamFactory->createStream($content);
37
    }
38
}
39