ResponseCastable   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 74
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B castResponseToType() 0 25 6
B detectAndCastResponseToType() 0 25 7
1
<?php
2
3
namespace Kaylyu\Alipay\Kernel\Traits;
4
5
use Kaylyu\Alipay\Kernel\Contracts\Arrayable;
6
use Kaylyu\Alipay\Kernel\Exceptions\InvalidArgumentException;
7
use Kaylyu\Alipay\Kernel\Exceptions\InvalidConfigException;
8
use Kaylyu\Alipay\Kernel\Http\Response;
9
use Kaylyu\Alipay\Kernel\Support\Collection;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * Trait ResponseCastable.
14
 */
15
trait ResponseCastable
16
{
17
    /**
18
     * @param \Psr\Http\Message\ResponseInterface $response
19
     * @param string|null $type
20
     * @param array $forceArrayKeys
21
     *
22
     * @return array|\Kaylyu\Alipay\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
23
     *
24
     * @throws \Kaylyu\Alipay\Kernel\Exceptions\InvalidConfigException
25
     * @throws \Kaylyu\Alipay\Kernel\Exceptions\Exception
26
     */
27
    protected function castResponseToType(ResponseInterface $response, $type = null, array $forceArrayKeys = [])
28
    {
29
        $response = Response::buildFromPsrResponse($response);
30
        $response->setForceArrayKeys($forceArrayKeys)->getBody()->rewind();
31
32
        switch ($type ?? 'array') {
33
            case 'collection':
34
                return $response->toCollection();
35
            case 'array':
36
                return $response->toArray();
37
            case 'object':
38
                return $response->toObject();
39
            case 'raw':
40
                return $response;
41
            default:
42
                if (!is_subclass_of($type, Arrayable::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Kaylyu\Alipay\Kernel\Contracts\Arrayable::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
43
                    throw new InvalidConfigException(sprintf(
44
                        'Config key "response_type" classname must be an instanceof %s',
45
                        Arrayable::class
46
                    ));
47
                }
48
49
                return new $type($response);
50
        }
51
    }
52
53
    /**
54
     * @param mixed $response
55
     * @param string|null $type
56
     *
57
     * @return array|\Kaylyu\Alipay\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
58
     *
59
     * @throws \Kaylyu\Alipay\Kernel\Exceptions\InvalidArgumentException
60
     * @throws \Kaylyu\Alipay\Kernel\Exceptions\InvalidConfigException
61
     * @throws \Kaylyu\Alipay\Kernel\Exceptions\Exception
62
     */
63
    protected function detectAndCastResponseToType($response, $type = null)
64
    {
65
        switch (true) {
66
            case $response instanceof ResponseInterface:
67
                $response = Response::buildFromPsrResponse($response);
68
69
                break;
70
            case $response instanceof Arrayable:
71
                $response = new Response(200, [], json_encode($response->toArray()));
72
73
                break;
74
            case ($response instanceof Collection) || is_array($response) || is_object($response):
75
                $response = new Response(200, [], json_encode($response));
76
77
                break;
78
            case is_scalar($response):
79
                $response = new Response(200, [], $response);
80
81
                break;
82
            default:
83
                throw new InvalidArgumentException(sprintf('Unsupported response type "%s"', gettype($response)));
84
        }
85
86
        return $this->castResponseToType($response, $type);
87
    }
88
}
89