ResponseCastable   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 91
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponseType() 0 4 1
A setResponseType() 0 4 1
B castResponseToType() 0 25 6
B detectAndCastResponseToType() 0 25 7
1
<?php
2
3
namespace CloudyCity\UCMarketingSDK\Kernel\Traits;
4
5
use CloudyCity\UCMarketingSDK\Kernel\Contracts\Arrayable;
6
use CloudyCity\UCMarketingSDK\Kernel\Exceptions\InvalidArgumentException;
7
use CloudyCity\UCMarketingSDK\Kernel\Http\Response;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Psr\Http\Message\ResponseInterface;
10
11
trait ResponseCastable
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $responseType;
17
18
    /**
19
     * @return string
20
     */
21
    protected function getResponseType()
22
    {
23
        return $this->responseType;
24
    }
25
26
    /**
27
     * @param string $responseType
28
     */
29
    protected function setResponseType($responseType)
30
    {
31
        $this->responseType = $responseType;
32
    }
33
34
    /**
35
     * @param ResponseInterface $response
36
     * @param string            $type
37
     *
38
     * @throws InvalidArgumentException
39
     *
40
     * @return array|Response|\Doctrine\Common\Collections\ArrayCollection|object|ResponseInterface
41
     */
42
    protected function castResponseToType(ResponseInterface $response, $type = 'array')
43
    {
44
        $response = Response::buildFromPsrResponse($response);
45
        $response->getBody()->rewind();
46
47
        switch ($type) {
48
            case 'collection':
49
                return $response->toCollection();
50
            case 'array':
51
                return $response->toArray();
52
            case 'object':
53
                return $response->toObject();
54
            case 'raw':
55
                return $response;
56
            default:
57
                if (!is_subclass_of($type, Arrayable::class)) {
58
                    throw new InvalidArgumentException(sprintf(
59
                        '"$responseType" classname must be an instanceof %s',
60
                        Arrayable::class
61
                    ));
62
                }
63
64
                return new $type($response);
65
        }
66
    }
67
68
    /**
69
     * @param mixed  $response
70
     * @param string $type
71
     *
72
     * @throws InvalidArgumentException
73
     *
74
     * @return array|\Doctrine\Common\Collections\ArrayCollection|object|\Psr\Http\Message\ResponseInterface|string
75
     */
76
    protected function detectAndCastResponseToType($response, $type = 'array')
77
    {
78
        switch (true) {
79
            case $response instanceof ResponseInterface:
80
                $response = Response::buildFromPsrResponse($response);
81
82
                break;
83
            case $response instanceof Arrayable:
84
                $response = new Response(200, [], json_encode($response->toArray()));
85
86
                break;
87
            case ($response instanceof ArrayCollection) || is_array($response) || is_object($response):
88
                $response = new Response(200, [], json_encode($response));
89
90
                break;
91
            case is_scalar($response):
92
                $response = new Response(200, [], (string) $response);
93
94
                break;
95
            default:
96
                throw new InvalidArgumentException(sprintf('Unsupported response type "%s"', gettype($response)));
97
        }
98
99
        return $this->castResponseToType($response, $type);
100
    }
101
}
102