Completed
Push — feature/EVO-11896-empty-object... ( 4869c6 )
by Narcotic
20:12
created

HashHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * HashHandler class file
4
 */
5
6
namespace Graviton\DocumentBundle\Serializer\Handler;
7
8
use JMS\Serializer\Context;
9
use JMS\Serializer\JsonDeserializationVisitor;
10
use JMS\Serializer\JsonSerializationVisitor;
11
use Graviton\DocumentBundle\Entity\Hash;
12
use Symfony\Component\HttpFoundation\RequestStack;
13
14
/**
15
 * Hash handler for JMS serializer
16
 *
17
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
18
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
19
 * @link     http://swisscom.ch
20
 */
21
class HashHandler
22
{
23
24
    /**
25
     * @var RequestStack
26
     */
27
    private $requestStack;
28
29 4
    public function __construct(RequestStack $requestStack)
30
    {
31 4
        $this->requestStack = $requestStack;
32 4
    }
33
34
    /**
35
     * Serialize Hash object
36
     *
37
     * @param JsonSerializationVisitor $visitor Visitor
38
     * @param Hash                     $data    Data
39
     * @param array                    $type    Type
40
     * @param Context                  $context Context
41
     * @return Hash
42
     */
43 2
    public function serializeHashToJson(
44
        JsonSerializationVisitor $visitor,
45
        Hash $data,
46
        array $type,
47
        Context $context
48
    ) {
49 2
        return new Hash($data);
50
    }
51
52
    /**
53
     * Deserialize Hash object
54
     *
55
     * @param JsonDeserializationVisitor $visitor Visitor
56
     * @param array                      $data    Data
57
     * @param array                      $type    Type
58
     * @param Context                    $context Context
59
     * @return Hash
60
     */
61 2
    public function deserializeHashFromJson(
62
        JsonDeserializationVisitor $visitor,
63
        array $data,
64
        array $type,
65
        Context $context
66
    ) {
67 2
        $currentPath = $context->getCurrentPath();
68 2
        $currentRequest = $this->requestStack->getCurrentRequest();
69 2
        $dataObj = null;
70
71 2
        var_dump($data);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($data); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
72 2
        var_dump($type);
73 2
        var_dump($currentPath);
74 2
        var_dump($context->attributes);
75 2
        echo "-----------";
76
77 2
        if (!is_null($currentRequest)) {
78
            $dataObj = json_decode($currentRequest->getContent());
79
            foreach ($currentPath as $pathElement) {
80
                if (isset($dataObj->{$pathElement})) {
81
                    $dataObj = $dataObj->{$pathElement};
82
                } else {
83
                    $dataObj = null;
84
                    break;
85
                }
86
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
87
            }
88
        }
89
90 2
        if (!is_null($dataObj)) {
91
            $data = $dataObj;
92
        }
93
94
        //return new Hash($data);
95
96
97 2
        return new Hash($visitor->visitArray((array) $data, $type, $context));
98
    }
99
}
100