Completed
Push — standalone ( 57bbfc...ea253f )
by Philip
03:00
created

RestRequestParser::__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
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Service;
4
5
use Doctrine\Common\Util\ClassUtils;
6
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata;
7
use Metadata\MetadataFactory;
8
use Symfony\Component\HttpFoundation\Request;
9
10
class RestRequestParser
11
{
12
    /**
13
     * @var MetadataFactory
14
     */
15
    private $ddrRestMetadataFactory;
16
17 2
    public function __construct(MetadataFactory $ddrRestMetadataFactory)
18
    {
19 2
        $this->ddrRestMetadataFactory = $ddrRestMetadataFactory;
20 2
    }
21
22
    /**
23
     * @param Request     $request
24
     * @param string      $entityClass
25
     * @param object|null $entity
26
     *
27
     * @return object
28
     */
29 2
    public function parseEntity(
30
        Request $request,
31
        $entityClass,
32
        $entity = null
33
    ) {
34 2
        $method = $request->getMethod();
35 2
        $content = $this->getRequestContent($request);
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
36 2
        $format = $request->getRequestFormat();
0 ignored issues
show
Unused Code introduced by
$format is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
37
38 2
        $data = $this->getRequestContent($request);
39
40 2
        if (null === $entity) {
41
            $entity = new $entityClass;
42
        }
43
44 2
        $this->updateObject($entity, $method, $data);
45
46 2
        return $entity;
47
    }
48
49
    /**
50
     * @param Request $request
51
     *
52
     * @return array
53
     */
54 2
    protected function getRequestContent(Request $request)
55
    {
56 2
        $content = $request->getContent();
57 2
        if ('' !== $content) {
58 2
            return json_decode($content, true);
59
        }
60
61
        return $request->request->all();
62
    }
63
64
    /**
65
     * @param object $object Access by reference.
66
     * @param string $method
67
     * @param array  $data
68
     */
69 2
    protected function updateObject(
70
        &$object,
71
        $method,
72
        $data
73
    ) {
74
75 2
        $classMetadata = $this->ddrRestMetadataFactory->getMetadataForClass(ClassUtils::getClass($object));
76
77 2
        foreach ($data as $key => $value) {
78 2
            if (!array_key_exists($key, $classMetadata->propertyMetadata)) {
79
                continue;
80
                //throw new \RuntimeException(sprintf('No field %s for Class %s', $key, get_class($object)));
81
            }
82
            /** @var PropertyMetadata $propertyMetadata */
83 2
            $propertyMetadata = $classMetadata->propertyMetadata[$key];
84 2
            if ($this->isUpdateable($method, $propertyMetadata)) {
85 2
                $this->updateProperty($object, $method, $key, $value, $propertyMetadata);
86
            }
87
        }
88 2
    }
89
90
    /**
91
     * @param object           $object Access by reference.
92
     * @param string           $method
93
     * @param string           $propertyName
94
     * @param mixed            $value
95
     * @param PropertyMetadata $propertyMetadata
96
     */
97 2
    protected function updateProperty(
98
        &$object,
99
        $method,
0 ignored issues
show
Unused Code introduced by
The parameter $method is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
        $propertyName,
0 ignored issues
show
Unused Code introduced by
The parameter $propertyName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
        $value,
102
        PropertyMetadata $propertyMetadata
103
    ) {
104
105
        // TODO: Reenable embedded
106
//        if (array_key_exists($propertyName, $doctrineClassMetadata->embeddedClasses)) {
107
//            $embeddedClass = $doctrineClassMetadata->embeddedClasses[$propertyName]['class'];
108
//            $this->updatePropertyObject($object, $method, $embeddedClass, $propertyName, $value);
109
//
110
//            return;
111
//        }
112
113
        //TODO: Reenable associations
114
//        if (array_key_exists($propertyName, $doctrineClassMetadata->associationMappings)) {
115
//            $associatedClass = $doctrineClassMetadata->associationMappings[$propertyName]['targetEntity'];
116
//            $this->updatePropertyObject($object, $method, $associatedClass, $propertyName, $value);
117
//
118
//            return;
119
//        }
120
121 2
        $convertedValue = $this->convert($propertyMetadata->getType(), $value);
122 2
        $propertyMetadata->setValue($object, $convertedValue);
123 2
    }
124
125
//    /**
126
//     * @param object $object Access by reference.
127
//     * @param string $method
128
//     * @param string $propertyName
129
//     * @param [] $value
130
//     */
131
//    protected function updatePropertyObject(
132
//        &$object,
133
//        $method,
134
//        $class,
135
//        $propertyName,
136
//        $value
137
//    ) {
138
//        $propertyObject = $this->propertyAccessor->getValue($object, $propertyName);
139
//        if (null === $propertyObject) {
140
//            $propertyObject = new $class;
141
//        }
142
//
143
//        $this->updateObject($propertyObject, $method, $value);
144
//        $this->propertyAccessor->setValue($object, $propertyName, $propertyObject);
145
//    }
146
147
    /**
148
     * @param string           $method
149
     * @param PropertyMetadata $propertyMetadata
150
     *
151
     * @return bool
152
     */
153 2
    protected function isUpdateable(
154
        $method,
155
        PropertyMetadata $propertyMetadata
156
    ) {
157 2
        if (Request::METHOD_PUT === $method || Request::METHOD_PATCH === $method) {
158 2
            return $propertyMetadata->isPuttable();
159
        }
160
        if (Request:: METHOD_POST === $method) {
161
            return $propertyMetadata->isPostable();
162
        }
163
164
        return false;
165
    }
166
167 2
    private function convert(?string $type, $value)
168
    {
169 2
        if (null === $value) {
170
            return $value;
171
        }
172
173
        switch ($type) {
174 2
            case 'datetime':
175 2
            case 'date':
176 2
            case 'time':
177 2
                return new \DateTime($value);
178
            default:
179
                return $value;
180
        }
181
    }
182
}
183