JsonSnsMessageSerializer::unserialize()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4285
ccs 7
cts 7
cp 1
nc 3
cc 3
eloc 8
nop 1
crap 3
1
<?php
2
3
namespace Abacaphiliac\AwsSdk\ClaimCheck\Serializer;
4
5
use Abacaphiliac\AwsSdk\ClaimCheck\ClaimCheck;
6
use Abacaphiliac\AwsSdk\ClaimCheck\Exception\ExceptionInterface;
7
use Abacaphiliac\AwsSdk\ClaimCheck\Exception\InvalidArgumentException;
8
use Zend\Json\Exception\RuntimeException;
9
use Zend\Json\Json;
10
11
class JsonSnsMessageSerializer implements ClaimCheckSerializerInterface
12
{
13
    /** @var  ClaimCheckJsonSerializer */
14
    private $serializer;
15
16
    /**
17
     * JsonSnsMessageSerializer constructor.
18
     */
19 23
    public function __construct()
20
    {
21 23
        $this->serializer = new ClaimCheckJsonSerializer();
22 23
    }
23
24
    /**
25
     * @param ClaimCheck $claimCheck
26
     * @return string
27
     * @throws ExceptionInterface
28
     */
29 1
    public function serialize(ClaimCheck $claimCheck)
30
    {
31 1
        return $this->serializer->serialize($claimCheck);
32
    }
33
34
    /**
35
     * @param string $encodedValue
36
     * @return ClaimCheck
37
     * @throws ExceptionInterface
38
     */
39 4
    public function unserialize($encodedValue)
40
    {
41
        try {
42 4
            $data = Json::decode($encodedValue, Json::TYPE_ARRAY);
43 4
        } catch (RuntimeException $e) {
44 2
            throw new InvalidArgumentException($e->getMessage(), 0, $e);
45
        }
46
        
47 2
        if (!array_key_exists('Message', $data)) {
48 1
            throw new InvalidArgumentException('Message is required and cannot be empty.');
49
        }
50
        
51 1
        return $this->serializer->unserialize($data['Message']);
52
    }
53
}
54