Passed
Pull Request — master (#3)
by Evgeny
06:38
created

ObjectConverter::responseToArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Lamoda\AtolClient\Converter;
4
5
use Lamoda\AtolClient\Exception\ParseException;
6
use Lamoda\AtolClient\Exception\ValidationException;
7
use JMS\Serializer\Context;
8
use JMS\Serializer\SerializationContext;
9
use JMS\Serializer\SerializerInterface;
10
use Symfony\Component\Validator\Validator\ValidatorInterface;
11
12
/**
13
 * Converts request and response from and to json.
14
 */
15
class ObjectConverter
16
{
17
    /**
18
     * @var SerializerInterface
19
     */
20
    private $serializer;
21
22
    /**
23
     * @var ValidatorInterface
24
     */
25
    private $validator;
26
27 27
    public function __construct(
28
        SerializerInterface $serializer,
29
        ValidatorInterface $validator
30
    ) {
31 27
        $this->serializer = $serializer;
32 27
        $this->validator = $validator;
33 27
    }
34
35
    /**
36
     * @param string $class
37
     * @param string $json
38
     *
39
     * @throws ValidationException
40
     * @throws ParseException
41
     *
42
     * @return mixed
43
     */
44 21
    public function getResponseObject(string $class, string $json)
45
    {
46 21
        $object = $this->deserialize($class, $json);
47 16
        $this->assertValid($object, ValidationException::RESPONSE);
48
49 13
        return $object;
50
    }
51
52
    /**
53
     * @param mixed $object
54
     *
55
     * @throws ValidationException
56
     * @throws ParseException
57
     *
58
     * @return string
59
     */
60 21
    public function getRequestString($object): string
61
    {
62 21
        $this->assertValid($object, ValidationException::RESPONSE);
63
64 18
        return $this->serializeBodyObject($object);
65
    }
66
67
    /**
68
     * @param string $class
69
     * @param string $json
70
     *
71
     * @throws ParseException
72
     *
73
     * @return mixed
74
     */
75 21
    private function deserialize(string $class, string $json)
76
    {
77
        try {
78 21
            return $this->serializer->deserialize($json, $class, 'atol_client');
79 5
        } catch (\RuntimeException $exception) {
80 3
            throw ParseException::becauseOfRuntimeException($exception, ParseException::RESPONSE);
81
        }
82
    }
83
84
    /**
85
     * Assert that object is valid.
86
     *
87
     * @param mixed $object
88
     * @param int $code
89
     *
90
     * @throws ValidationException
91
     */
92 26
    private function assertValid($object, int $code = 0)
93
    {
94 26
        $errors = $this->validator->validate($object);
95 26
        if (count($errors)) {
96 6
            throw ValidationException::becauseOfValidationErrors($errors, $code);
97
        }
98 22
    }
99
100
    /**
101
     * @param mixed $object
102
     *
103
     * @throws ParseException
104
     *
105
     * @return string
106
     */
107 18
    private function serializeBodyObject($object): string
108
    {
109
        try {
110 18
            return $this->serializer->serialize($object, 'atol_client', $this->getSerializeBodyObjectContext());
111 1
        } catch (\RuntimeException $exception) {
112 1
            throw ParseException::becauseOfRuntimeException($exception, ParseException::REQUEST);
113
        }
114
    }
115
116
    /**
117
     * @return Context
118
     */
119 18
    private function getSerializeBodyObjectContext(): Context
120
    {
121 18
        return SerializationContext::create()->setGroups(['Default', 'post']);
122
    }
123
}
124