DoctrineResolver::resolveJsonField()   C
last analyzed

Complexity

Conditions 12
Paths 2

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 6.9666
c 0
b 0
f 0
cc 12
nc 2
nop 5

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
namespace Doctrine\ODM\CouchDB\Mapping\MetadataResolver;
5
6
use Doctrine\ODM\CouchDB\Mapping\ClassMetadata;
7
use Doctrine\ODM\CouchDB\DocumentManager;
8
use Doctrine\ODM\CouchDB\PersistentIdsCollection;
9
use Doctrine\Common\Collections\ArrayCollection;
10
11
class DoctrineResolver implements MetadataResolver
12
{
13
    public function createDefaultDocumentStruct(ClassMetadata $class)
14
    {
15
        $struct = array('type' => str_replace("\\", ".", $class->name));
16
        if ($class->indexed) {
17
            $struct['doctrine_metadata']['indexed'] = true;
18
        }
19
        if ($class->indexes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $class->indexes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
20
            $struct['doctrine_metadata']['indexes'] = $class->indexes;
21
        }
22
        return $struct;
23
    }
24
25
    public function canResolveJsonField($jsonName)
26
    {
27
        return ($jsonName === 'doctrine_metadata');
28
    }
29
30
    public function getDocumentType(array $documentData)
31
    {
32
        return (str_replace(".", "\\", $documentData['type']));
33
    }
34
35
    public function resolveJsonField(ClassMetadata $class, DocumentManager $dm, $documentState, $jsonName, $originalData)
36
    {
37
        $uow = $dm->getUnitOfWork();
38
        $couchClient = $dm->getCouchDBClient();
39
40
        if ($jsonName == 'doctrine_metadata' && isset($originalData['doctrine_metadata']['associations'])) {
41
            foreach ($originalData['doctrine_metadata']['associations'] AS $assocName) {
42
                $assocValue = $originalData[$assocName];
43
                if (isset($class->associationsMappings[$assocName])) {
44
                    if ($class->associationsMappings[$assocName]['type'] & ClassMetadata::TO_ONE) {
45
                        if ($assocValue) {
46
                            if ($class->associationsMappings[$assocName]['targetDocument'] &&
47
                                ! $dm->getClassMetadata($class->associationsMappings[$assocName]['targetDocument'])->inInheritanceHierachy) {
48
49
                                $assocValue = $dm->getReference($class->associationsMappings[$assocName]['targetDocument'], $assocValue);
50
                            } else {
51
                                $response = $couchClient->findDocument($assocValue);
52
53
                                if ($response->status == 404) {
54
                                    $assocValue = null;
55
                                } else {
56
                                    $hints = array();
57
                                    $assocValue = $uow->createDocument(null, $response->body, $hints);
58
                                }
59
                            }
60
                        }
61
                        $documentState[$class->associationsMappings[$assocName]['fieldName']] = $assocValue;
62
                    } else if ($class->associationsMappings[$assocName]['type'] & ClassMetadata::MANY_TO_MANY) {
63
                        if ($class->associationsMappings[$assocName]['isOwning']) {
64
                            $documentState[$class->associationsMappings[$assocName]['fieldName']] = new PersistentIdsCollection(
65
                                new ArrayCollection(),
66
                                $class->associationsMappings[$assocName]['targetDocument'],
67
                                $dm,
68
                                $assocValue
69
                            );
70
                        }
71
                    }
72
                }
73
            }
74
        }
75
        return $documentState;
76
    }
77
78
    public function canMapDocument(array $documentData)
79
    {
80
        return isset($documentData['type']);
81
    }
82
83
    public function storeAssociationField($data, ClassMetadata $class, DocumentManager $dm, $fieldName, $fieldValue)
84
    {
85
        $data['doctrine_metadata']['associations'][] = $fieldName;
86
        $data[$fieldName] = $fieldValue;
87
        return $data;
88
    }
89
}
90