Completed
Push — develop ( 97a737...3a270c )
by Fabian
29s queued 18s
created

JsonResponse::serialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cakasim\Payone\Sdk\Api\Message;
6
7
/**
8
 * @author Fabian Böttcher <[email protected]>
9
 * @since 0.1.0
10
 */
11
class JsonResponse implements JsonResponseInterface
12
{
13
    /**
14
     * @var array The JSON response data.
15
     */
16
    protected $json = [];
17
18
    /**
19
     * @inheritDoc
20
     */
21
    public function serialize(): string
22
    {
23
        $encoded = json_encode($this->json);
24
25
        if (!is_string($encoded)) {
0 ignored issues
show
introduced by
The condition is_string($encoded) is always true.
Loading history...
26
            throw new \RuntimeException("Cannot serialize JSON response message, failed encoding of JSON data.");
27
        }
28
29
        return $encoded;
30
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35
    public function unserialize($serialized): void
36
    {
37
        $this->json = json_decode($serialized, true);
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public function parseResponseData($data): void
44
    {
45
        if (!is_string($data)) {
46
            throw new \RuntimeException(sprintf("Cannot parse response data of type '%s', expected a string.", gettype($data)));
47
        }
48
49
        $json = json_decode($data, true);
50
51
        if (!is_array($json)) {
52
            throw new \RuntimeException(sprintf("Failed decoding of JSON response data: %s", json_last_error_msg()));
53
        }
54
55
        $this->json = $json;
56
    }
57
}
58