ResponseCastable::castResponseToType()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 6
nop 2
dl 0
loc 23
rs 9.0777
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the dingtalk.
4
 * User: Ilham Tahir <[email protected]>
5
 * This source file is subject to the MIT license that is bundled
6
 * with this source code in the file LICENSE.
7
 */
8
9
namespace Aplisin\DingTalk\Kernel\Traits;
10
11
use Aplisin\DingTalk\Kernel\Contracts\Arrayable;
12
use Aplisin\DingTalk\Kernel\Exceptions\InvalidArgumentException;
13
use Aplisin\DingTalk\Kernel\Exceptions\InvalidConfigException;
14
use Aplisin\DingTalk\Kernel\Support\Collection;
15
use Psr\Http\Message\ResponseInterface;
16
use Aplisin\DingTalk\Kernel\Http\Response;
17
18
trait ResponseCastable
19
{
20
    /**
21
     * @param ResponseInterface $response
22
     * @param null $type
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $type is correct as it would always require null to be passed?
Loading history...
23
     * @return Response|Collection|array|mixed|ResponseInterface
24
     * @throws InvalidConfigException
25
     */
26
    protected function castResponseToType(ResponseInterface $response, $type = null)
27
    {
28
        $response = Response::buildFromPsrResponse($response);
29
        $response->getBody()->rewind();
30
31
        switch ($type ?? 'array') {
32
            case 'collection':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
33
                return $response->toCollection();
34
            case 'array':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
35
                return $response->toArray();
36
            case 'object':
37
                return $response->toObject();
38
            case 'raw':
39
                return $response;
40
            default:
41
                if (!is_subclass_of($type, Arrayable::class)) {
42
                    throw new InvalidConfigException(sprintf(
43
                        'Config key "response_type" classname must be an instanceof %s',
44
                        Arrayable::class
45
                    ));
46
                }
47
48
                return new $type($response);
49
        }
50
    }
51
52
    /**
53
     * @param $response
54
     * @param null $type
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $type is correct as it would always require null to be passed?
Loading history...
55
     * @return ResponseCastable|array|mixed|ResponseInterface
56
     * @throws InvalidArgumentException
57
     * @throws InvalidConfigException
58
     */
59
    protected function detectAndCastResponseToType($response, $type = null)
60
    {
61
        switch (true) {
62
            case $response instanceof ResponseInterface:
63
                $response = Response::buildFromPsrResponse($response);
64
65
                break;
66
            case ($response instanceof Collection) || is_array($response) || is_object($response):
67
                $response = new Response(200, [], json_encode($response));
68
69
                break;
70
            case is_scalar($response):
71
                $response = new Response(200, [], $response);
72
73
                break;
74
            default:
75
                throw new InvalidArgumentException(sprintf('Unsupported response type "%s"', gettype($response)));
76
        }
77
78
        return $this->castResponseToType($response, $type);
79
    }
80
}
81