Passed
Push — v3.x ( 054475...230925 )
by Chad
04:27
created

Serializer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 47
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A unserialize() 0 10 1
A serialize() 0 17 1
1
<?php
2
3
namespace Chadicus\Marvel\Api\Cache;
4
5
use GuzzleHttp\Psr7\Response;
6
use Chadicus\Psr\SimpleCache\SerializerInterface;
7
use DominionEnterprises\Util;
8
9
/**
10
 * Provides serialization from mongo documents to PSR-7 response objects.
11
 */
12
final class Serializer implements SerializerInterface
13
{
14
    /**
15
     * Unserializes cached data into the original state.
16
     *
17
     * @param array $data The data to unserialize.
18
     *
19
     * @return Response
20
     */
21
    public function unserialize(array $data)
22
    {
23
        return new Response(
24
            $data['statusCode'],
25
            $data['headers'],
26
            $data['body'],
27
            $data['protocolVersion'],
28
            $data['reasonPhrase']
29
        );
30
    }
31
32
    /**
33
     * Serializes the given data for storage in caching.
34
     *
35
     * @param mixed $value The data to serialize for caching.
36
     *
37
     * @return array The result of serializing the given $data.
38
     *
39
     * @throws \Psr\SimpleCache\InvalidArgumentException Thrown if the given value is not a PSR-7 Response instance.
40
     */
41
    public function serialize($value) : array
42
    {
43
        Util::ensure(
44
            true,
45
            is_a($value, '\\Psr\\Http\\Message\\ResponseInterface'),
46
            '\\Chadicus\\Psr\\SimpleCache\\InvalidArgumentException',
47
            ['$value must be an instance of \\Psr\\Http\\Message\\ResponseInterface']
48
        );
49
50
        return [
51
            'statusCode' => $value->getStatusCode(),
52
            'headers' => $value->getHeaders(),
53
            'body' => (string)$value->getBody(),
54
            'protocolVersion' => $value->getProtocolVersion(),
55
            'reasonPhrase' => $value->getReasonPhrase(),
56
        ];
57
    }
58
}
59