ClaimCheckJsonSerializer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 3
dl 0
loc 37
rs 10
ccs 14
cts 14
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 7 1
B unserialize() 0 16 5
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 ClaimCheckJsonSerializer implements ClaimCheckSerializerInterface
12
{
13
    /**
14
     * @param ClaimCheck $claimCheck
15
     * @return string
16
     * @throws ExceptionInterface
17
     */
18 6
    public function serialize(ClaimCheck $claimCheck)
19
    {
20 6
        return Json::encode(array(
21 6
            's3BucketName' => $claimCheck->getS3BucketName(),
22 6
            's3Key' => $claimCheck->getS3Key(),
23 6
        ));
24
    }
25
26
    /**
27
     * @param string $encodedValue
28
     * @return ClaimCheck
29
     * @throws ExceptionInterface
30
     */
31 8
    public function unserialize($encodedValue)
32
    {
33
        try {
34 8
            $data = Json::decode($encodedValue, Json::TYPE_ARRAY);
35 8
        } catch (RuntimeException $e) {
36 2
            throw new InvalidArgumentException($e->getMessage(), 0, $e);
37
        }
38
        
39 6
        foreach (array('s3BucketName', 's3Key') as $param) {
40 6
            if (!array_key_exists($param, $data) || !$data[$param]) {
41 2
                throw new InvalidArgumentException(sprintf('Param %s is required and cannot be empty.', $param));
42
            }
43 5
        }
44
        
45 4
        return new ClaimCheck($data['s3BucketName'], $data['s3Key']);
46
    }
47
}
48