Passed
Pull Request — master (#1647)
by
11:06
created

ResponseCastable::castResponseToType()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 17
c 2
b 0
f 0
nc 6
nop 2
dl 0
loc 23
rs 9.0777
ccs 17
cts 17
cp 1
crap 6
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\Kernel\Traits;
13
14
use EasyWeChat\Kernel\Contracts\Arrayable;
15
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
16
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
17
use EasyWeChat\Kernel\Http\Response;
18
use EasyWeChat\Kernel\Support\Collection;
19
use Psr\Http\Message\ResponseInterface;
20
21
/**
22
 * Trait ResponseCastable.
23
 *
24
 * @author overtrue <[email protected]>
25
 */
26
trait ResponseCastable
27
{
28
    /**
29
     * @param \Psr\Http\Message\ResponseInterface $response
30
     * @param string|null                         $type
31
     *
32
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
33
     *
34
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
35
     */
36 23
    protected function castResponseToType(ResponseInterface $response, $type = null)
37
    {
38 23
        $response = Response::buildFromPsrResponse($response);
39 23
        $response->getBody()->rewind();
40
41 23
        switch ($type ?? 'array') {
42 23
            case 'collection':
43 3
                return $response->toCollection();
44 22
            case 'array':
45 22
                return $response->toArray();
46 2
            case 'object':
47 1
                return $response->toObject();
48 2
            case 'raw':
49 2
                return $response;
50
            default:
51 1
                if (!is_subclass_of($type, Arrayable::class)) {
52 1
                    throw new InvalidConfigException(sprintf(
53 1
                        'Config key "response_type" classname must be an instanceof %s',
54 1
                        Arrayable::class
55
                    ));
56
                }
57
58 1
                return new $type($response);
59
        }
60
    }
61
62
    /**
63
     * @param mixed       $response
64
     * @param string|null $type
65
     *
66
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
67
     *
68
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
69
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
70
     */
71 8
    protected function detectAndCastResponseToType($response, $type = null)
72
    {
73
        switch (true) {
74 8
            case $response instanceof ResponseInterface:
75 1
                $response = Response::buildFromPsrResponse($response);
76
77 1
                break;
78 8
            case $response instanceof Arrayable:
79 1
                $response = new Response(200, [], json_encode($response->toArray()));
80
81 1
                break;
82 8
            case ($response instanceof Collection) || is_array($response) || is_object($response):
83 8
                $response = new Response(200, [], json_encode($response));
84
85 8
                break;
86 1
            case is_scalar($response):
87 1
                $response = new Response(200, [], $response);
88
89 1
                break;
90
            default:
91 1
                throw new InvalidArgumentException(sprintf('Unsupported response type "%s"', gettype($response)));
92
        }
93
94 8
        return $this->castResponseToType($response, $type);
95
    }
96
}
97