Passed
Push — master ( bdfaa2...ebdd6d )
by Vladislav
51s queued 13s
created

CurlResponseHandler::handle()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 47
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 47
rs 8.0555
cc 9
nc 13
nop 2
1
<?php
2
namespace Carpenstar\ByBitAPI\Core\Response;
3
4
use Carpenstar\ByBitAPI\Core\Enums\EnumOutputMode;
5
use Carpenstar\ByBitAPI\Core\Exceptions\ApiException;
6
use Carpenstar\ByBitAPI\Core\Builders\ResponseDtoBuilder;
7
use Carpenstar\ByBitAPI\Core\Interfaces\ICurlResponseDtoInterface;
8
use Carpenstar\ByBitAPI\Core\Interfaces\IResponseHandlerInterface;
9
use Carpenstar\ByBitAPI\Core\Objects\Collection\ArrayCollection;
10
use Carpenstar\ByBitAPI\Core\Objects\Collection\EntityCollection;
11
use Carpenstar\ByBitAPI\Core\Objects\Collection\StringCollection;
12
13
class CurlResponseHandler implements IResponseHandlerInterface
14
{
15
    /**
16
     * @var string $entityClassName
17
     */
18
    private string $entity;
19
20
    /**
21
     * @param string $className
22
     * @return void
23
     */
24
    public function bindEntity(string $className): self
25
    {
26
        $this->entity = $className;
27
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Carpenstar\ByBitAPI\Core...nse\CurlResponseHandler which is incompatible with the documented return type void.
Loading history...
28
    }
29
30
    /**
31
     * @return IResponseHandlerInterface
32
     * @throws \Exception
33
     */
34
    public function handle(string $apiData, int $mode = EnumOutputMode::MODE_ARRAY): ICurlResponseDtoInterface
35
    {
36
        $data = json_decode($apiData, true);
37
38
        if (!empty($data["retCode"]) && (int)$data["retCode"] > 0) {
39
            throw new ApiException($data["retMsg"], $data["retCode"]);
40
        }
41
42
        $time = $data['time'];
43
        $code = $data['retCode'];
44
        $msg = $data['retMsg'];
45
        $extMsg = $data['retExtInfo'];
46
47
        if (isset($data['result'][$this->entity::$rootDataKey])) {
0 ignored issues
show
Bug introduced by
The property rootDataKey does not exist on string.
Loading history...
48
            $data = $data['result'][$this->entity::$rootDataKey];
49
        } elseif (!empty($data['result'])) {
50
            $data = [$data['result']];
51
        }
52
53
        switch ($mode) {
54
            case EnumOutputMode::MODE_JSON:
55
                $collection = new StringCollection();
56
                $collection->push($apiData);
57
                break;
58
            case EnumOutputMode::MODE_ARRAY:
59
                $collection = new ArrayCollection();
60
                array_walk($data, function ($item) use ($collection) {
61
                    $collection->push($item);
62
                });
63
                break;
64
            case EnumOutputMode::MODE_ENTITY:
65
            default:
66
                $collection = new EntityCollection();
67
                array_walk($data, function ($item) use ($collection) {
68
                    if (!empty($item)) {
69
                        $collection->push(ResponseDtoBuilder::make($this->entity, $item));
70
                    }
71
                });
72
                break;
73
        }
74
75
        return (new CurlResponseDto())
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Carpenstar\By...)->setBody($collection) returns the type Carpenstar\ByBitAPI\Core\Response\CurlResponseDto which is incompatible with the documented return type Carpenstar\ByBitAPI\Core...esponseHandlerInterface.
Loading history...
76
            ->setTime($time)
77
            ->setReturnCode($code)
78
            ->setReturnMessage($msg)
79
            ->setReturnExtendedInfo($extMsg)
80
            ->setBody($collection);
81
    }
82
}