JsonSnsMessageSerializer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 43
rs 10
ccs 12
cts 12
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serialize() 0 4 1
A unserialize() 0 14 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