deserializeRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php
2
3
namespace JDesrosiers\Silex\Provider\ContentNegotiation;
4
5
use Silex\Application;
6
use Symfony\Component\HttpFoundation\Response;
7
8
/**
9
 * This class provides helper methods for serializing responses and deserializing requests using JMS Serializer.
10
 */
11
class JmsSerializerContentNegotiation implements ContentNegotiation
12
{
13
    protected $app;
14
15
    /**
16
     * @param Application $app
17
     */
18 6
    public function __construct(Application $app)
19
    {
20 6
        $this->app = $app;
21 6
    }
22
23
    /**
24
     * This method can be used in place of Response::create to automatically serialize your response into the requested
25
     * format.
26
     *
27
     * @param object $responseObject
28
     * @param int $status
29
     * @param array $headers
30
     * @return Response
31
     */
32 3
    public function createResponse($responseObject, $status = 200, array $headers = array())
33
    {
34 3
        $request = $this->app["request_stack"]->getCurrentRequest();
35 3
        $format = $request->getRequestFormat($this->app["conneg.defaultFormat"]);
36
37 3
        $serializedContent = $this->app["serializer"]->serialize(
38 3
            $responseObject,
39 3
            $format,
40 3
            $this->app["conneg.serializationContext"]
41 3
        );
42
43 3
        return new Response($serializedContent, $status, $headers);
44
    }
45
46
    /**
47
     * This method retrieves the request body in any supported format and deserializes it.
48
     *
49
     * @param string $className
50
     * @return object
51
     */
52 3
    public function deserializeRequest($className)
53
    {
54 3
        $request = $this->app["request_stack"]->getCurrentRequest();
55
56 3
        return $this->app["serializer"]->deserialize(
57 3
            $request->getContent(),
58 3
            $className,
59 3
            $request->getContentType(),
60 3
            $this->app["conneg.deserializationContext"]
61 3
        );
62
    }
63
}
64