ApiResponseFormatter   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 2
eloc 10
c 4
b 0
f 0
dl 0
loc 22
ccs 9
cts 9
cp 1
rs 10

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\Formatter;
6
7
use App\Factory\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 14
    public function __construct(
19
        ApiResponseDataFactory $apiResponseDataFactory,
20
        JsonDataResponseFormatter $jsonDataResponseFormatter
21
    ) {
22 14
        $this->apiResponseDataFactory = $apiResponseDataFactory;
23 14
        $this->jsonDataResponseFormatter = $jsonDataResponseFormatter;
24
    }
25
26 14
    public function format(DataResponse $dataResponse): ResponseInterface
27
    {
28 14
        $response = $dataResponse->withData(
29 14
            $this->apiResponseDataFactory
30 14
                ->createFromResponse($dataResponse)
31 14
                ->toArray()
32
        );
33
34 14
        return $this->jsonDataResponseFormatter->format($response);
35
    }
36
}
37