RequestHandler::deserialize()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 3
1
<?php
2
3
namespace Realshadow\RequestDeserializer\Http\Request;
4
5
use Realshadow\RequestDeserializer\Contracts\RequestInterface;
6
use JMS\Serializer\DeserializationContext;
7
use JMS\Serializer\Serializer;
8
9
10
/**
11
 * Handles incomming requests
12
 *
13
 * @package Realshadow\RequestDeserializer\Http\Request
14
 * @author Lukáš Homza <[email protected]>
15
 */
16
class RequestHandler
17
{
18
19
    /**
20
     * @var Serializer $serializer
21
     */
22
    private $serializer;
23
24
    /**
25
     * @var RequestInterface $entity
26
     */
27
    private $entity;
28
29
    /**
30
     * @var int $argumentPosition
31
     */
32
    private $argumentPosition = 0;
33
34
    /**
35
     * @var bool $bound
36
     */
37
    private $bound = false;
38
39
    /**
40
     * @param Serializer $serializer
41
     */
42
    public function __construct(Serializer $serializer)
43
    {
44
        $this->serializer = $serializer;
45
    }
46
47
    /**
48
     * Method will deserialize request content and bind it to the provided entity
49
     *
50
     * @param string $entityClass
51
     * @param string $content
52
     * @param DeserializationContext $context
53
     */
54
    public function deserialize($entityClass, $content, DeserializationContext $context = null)
55
    {
56
        if ( ! $context) {
57
            $context = DeserializationContext::create()
58
                ->setVersion(call_user_func($entityClass . '::getVersionConstraint'));
59
        }
60
61
        if ( ! $content) {
62
            $content = '{}';
63
        }
64
65
        $this->entity = $this->serializer->deserialize($content, $entityClass, 'json', $context);
66
        $this->bound = true;
67
    }
68
69
    /**
70
     * Hydrating request objects from arrays
71
     *
72
     * @param string $entityClass
73
     * @param array $content
74
     */
75
    public function hydrate($entityClass, array $content)
76
    {
77
        $context = DeserializationContext::create()
78
            ->setSerializeNull(false)
79
            ->setVersion(call_user_func($entityClass . '::getVersionConstraint'))
80
            ->setAttribute(
81
                RequestDeserializationHandler::NO_VALIDATE,
82
                ! call_user_func($entityClass . '::shouldValidate')
83
            );
84
85
86
        $this->deserialize($entityClass, json_encode($content), $context);
87
    }
88
89
    /**
90
     * Position of our entity in method parameters
91
     *
92
     * @param int $position
93
     * @return $this
94
     */
95
    public function setArgumentPosition($position)
96
    {
97
        $this->argumentPosition = $position;
98
99
        return $this;
100
    }
101
102
    /**
103
     * Get the deserialized entity
104
     *
105
     * @return null|RequestInterface
106
     */
107
    public function getBoundRequest()
108
    {
109
        return $this->entity;
110
    }
111
112
    /**
113
     * Did we manage to find an entity for this request?
114
     *
115
     * @return bool
116
     */
117
    public function isBound()
118
    {
119
        return $this->bound;
120
    }
121
122
    /**
123
     * We inject our deserialized entity into method parameters
124
     *
125
     * @param $parameters
126
     * @return array
127
     */
128
    public function bindRequest(array $parameters)
129
    {
130
        # -- recalculate due to mixed keys
131
        $parameters = array_values($parameters);
132
133
        if ($parameters !== null && is_array($parameters)) {
134
            $parameters[$this->argumentPosition] = $this->entity;
135
        }
136
137
        return $parameters;
138
    }
139
140
}
141