DataRequestParser::parse()   C
last analyzed

Complexity

Conditions 8
Paths 4

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
ccs 26
cts 26
cp 1
rs 5.3846
cc 8
eloc 23
nc 4
nop 1
crap 8
1
<?php
2
3
/*
4
 * This file is part of the PostmanGeneratorBundle package.
5
 *
6
 * (c) Vincent Chalamon <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PostmanGeneratorBundle\RequestParser;
13
14
use Doctrine\Common\Annotations\Reader;
15
use Dunglas\ApiBundle\Mapping\ClassMetadataFactoryInterface;
16
use PostmanGeneratorBundle\Faker\Guesser\Guesser;
17
use PostmanGeneratorBundle\Model\Request;
18
19
class DataRequestParser implements RequestParserInterface
20
{
21
    /**
22
     * @var ClassMetadataFactoryInterface
23
     */
24
    private $classMetadataFactory;
25
26
    /**
27
     * @var Reader
28
     */
29
    private $reader;
30
31
    /**
32
     * @var Guesser
33
     */
34
    private $guesser;
35
36
    /**
37
     * @param ClassMetadataFactoryInterface $classMetadataFactory
38
     * @param Reader                        $reader
39
     * @param Guesser                       $guesser
40
     */
41 8
    public function __construct(ClassMetadataFactoryInterface $classMetadataFactory, Reader $reader, Guesser $guesser)
42
    {
43 8
        $this->classMetadataFactory = $classMetadataFactory;
44 8
        $this->reader = $reader;
45 8
        $this->guesser = $guesser;
46 8
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 6
    public function parse(Request $request)
52
    {
53 6
        $resource = $request->getResource();
54 6
        $classMetadata = $this->classMetadataFactory->getMetadataFor(
55 6
            $resource->getEntityClass(),
56 6
            $resource->getNormalizationGroups(),
57 6
            $resource->getDenormalizationGroups(),
58 6
            $resource->getValidationGroups()
59 6
        );
60 6
        $request->addHeader('Content-Type', 'application/json');
61 6
        $request->setDataMode(Request::DATA_MODE_RAW);
62
63 6
        $rawModeData = [];
64 6
        foreach ($classMetadata->getAttributes() as $attributeMetadata) {
65 6
            if (!$classMetadata->getReflectionClass()->hasProperty($attributeMetadata->getName())) {
66
                // Attribute is not a property: ignore it
67 1
                continue;
68
            }
69 5
            $groups = $this->reader->getPropertyAnnotation(
70 5
                $classMetadata->getReflectionClass()->getProperty($attributeMetadata->getName()),
71
                'Symfony\Component\Serializer\Annotation\Groups'
72 5
            );
73
            if (
74 5
                $attributeMetadata->isIdentifier() ||
75 4
                !$attributeMetadata->isReadable() ||
76 3
                !count(array_intersect($groups ? $groups->getGroups() : [], $resource->getDenormalizationGroups() ?: []))
77 5
            ) {
78 4
                continue;
79
            }
80
81 1
            $rawModeData[$attributeMetadata->getName()] = $this->guesser->guess($attributeMetadata);
82 6
        }
83 6
        $request->setRawModeData($rawModeData);
84 6
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 2
    public function supports(Request $request)
90
    {
91 2
        return in_array($request->getMethod(), ['POST', 'PUT', 'PATCH']);
92
    }
93
}
94