ClaimCheckJsonSerializer::serialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
ccs 5
cts 5
cp 1
nc 1
cc 1
eloc 4
nop 1
crap 1
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