JmsSerializerContentNegotiation   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createResponse() 0 13 1
A deserializeRequest() 0 11 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