Passed
Pull Request — master (#545)
by Dmitriy
01:31
created

ApiResponseDataFactory::createFromResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Factory;
6
7
use App\Dto\ApiResponseData;
8
use Yiisoft\DataResponse\DataResponse;
0 ignored issues
show
Bug introduced by
The type Yiisoft\DataResponse\DataResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Yiisoft\Http\Status;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Http\Status was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
final class ApiResponseDataFactory
12
{
13
    public function createFromResponse(DataResponse $response): ApiResponseData
14
    {
15
        if ($response->getStatusCode() !== Status::OK) {
16
            return $this
17
                ->createErrorResponse()
18
                ->setErrorCode($response->getStatusCode())
19
                ->setErrorMessage($this->getErrorMessage($response));
20
        }
21
22
        return $this
23
            ->createSuccessResponse()
24
            ->setData($response->getData());
25
    }
26
27
    public function createSuccessResponse(): ApiResponseData
28
    {
29
        return $this
30
            ->createResponse()
31
            ->setStatus('success');
32
    }
33
34
    public function createErrorResponse(): ApiResponseData
35
    {
36
        return $this
37
            ->createResponse()
38
            ->setStatus('failed');
39
    }
40
41
    public function createResponse(): ApiResponseData
42
    {
43
        return new ApiResponseData();
44
    }
45
46
    private function getErrorMessage(DataResponse $response): string
47
    {
48
        $data = $response->getData();
49
        if (is_string($data) && !empty($data)) {
50
            return $data;
51
        }
52
53
        return 'Unknown error';
54
    }
55
}
56