SymfonySerializerContentNegotiation   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
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 47
ccs 15
cts 15
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 7 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 Symfony Serializer.
10
 */
11
class SymfonySerializerContentNegotiation 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
        return new Response($this->app['serializer']->serialize($responseObject, $format), $status, $headers);
38
    }
39
40
    /**
41
     * This method retrieves the request body in any supported format and deserializes it.
42
     *
43
     * @param string $className
44
     * @return object
45
     */
46 3
    public function deserializeRequest($className)
47
    {
48 3
        $request = $this->app["request_stack"]->getCurrentRequest();
49
50 3
        return $this->app["serializer"]->deserialize(
51 3
            $request->getContent(),
52 3
            $className,
53 3
            $request->getContentType(),
54 3
            $this->app["conneg.deserializationContext"]
55 3
        );
56
    }
57
}
58