Test Failed
Pull Request — master (#87)
by Dmitriy
02:46
created

ApiResponseFormatter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 22
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A format() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Infrastructure\Http\Formatter;
6
7
use App\Infrastructure\Http\Response\ApiResponseDataFactory;
8
use Psr\Http\Message\ResponseInterface;
9
use Yiisoft\DataResponse\DataResponse;
10
use Yiisoft\DataResponse\DataResponseFormatterInterface;
11
use Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter;
12
13
final class ApiResponseFormatter implements DataResponseFormatterInterface
14
{
15
    private ApiResponseDataFactory $apiResponseDataFactory;
16
    private JsonDataResponseFormatter $jsonDataResponseFormatter;
17
18
    public function __construct(
19
        ApiResponseDataFactory $apiResponseDataFactory,
20
        JsonDataResponseFormatter $jsonDataResponseFormatter
21
    ) {
22
        $this->apiResponseDataFactory = $apiResponseDataFactory;
23
        $this->jsonDataResponseFormatter = $jsonDataResponseFormatter;
24
    }
25
26
    public function format(DataResponse $dataResponse): ResponseInterface
27
    {
28
        $response = $dataResponse->withData(
29
            $this->apiResponseDataFactory
30
                ->createFromResponse($dataResponse)
31
                ->toArray()
32
        );
33
34
        return $this->jsonDataResponseFormatter->format($response);
35
    }
36
}
37