ApiResponseDataFactory::getErrorMessage()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 10
cc 3
nc 2
nop 1
crap 3.072
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Factory;
6
7
use App\Dto\ApiResponseData;
8
use Yiisoft\DataResponse\DataResponse;
9
use Yiisoft\Http\Status;
10
11
final class ApiResponseDataFactory
12
{
13 14
    public function createFromResponse(DataResponse $response): ApiResponseData
14
    {
15 14
        if ($response->getStatusCode() !== Status::OK) {
16
            return $this
17 7
                ->createErrorResponse()
18 7
                ->setErrorCode($response->getStatusCode())
19 7
                ->setErrorMessage($this->getErrorMessage($response));
20
        }
21
22
        return $this
23 7
            ->createSuccessResponse()
24 7
            ->setData($response->getData());
0 ignored issues
show
Bug introduced by
It seems like $response->getData() can also be of type object; however, parameter $data of App\Dto\ApiResponseData::setData() does only seem to accept array|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
            ->setData(/** @scrutinizer ignore-type */ $response->getData());
Loading history...
25
    }
26
27 7
    public function createSuccessResponse(): ApiResponseData
28
    {
29
        return $this
30 7
            ->createResponse()
31 7
            ->setStatus('success');
32
    }
33
34 7
    public function createErrorResponse(): ApiResponseData
35
    {
36
        return $this
37 7
            ->createResponse()
38 7
            ->setStatus('failed');
39
    }
40
41 14
    public function createResponse(): ApiResponseData
42
    {
43 14
        return new ApiResponseData();
44
    }
45
46 7
    private function getErrorMessage(DataResponse $response): string
47
    {
48 7
        $data = $response->getData();
49 7
        if (is_string($data) && !empty($data)) {
50 7
            return $data;
51
        }
52
53
        return 'Unknown error';
54
    }
55
}
56