MongoYamlFixture::createObject()   C
last analyzed

Complexity

Conditions 15
Paths 20

Size

Total Lines 70
Code Lines 44

Duplication

Lines 3
Ratio 4.29 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 3
loc 70
rs 5.6064
c 2
b 0
f 0
cc 15
eloc 44
nc 20
nop 4

How to fix   Long Method    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
namespace Khepin\YamlFixturesBundle\Fixture;
4
5
use Doctrine\Common\Util\Inflector;
6
7
class MongoYamlFixture extends AbstractFixture
8
{
9
    /**
10
     * Creates and returns one object based on the given data and metadata
11
     *
12
     * @param $class object's class name
13
     * @param $data array of the object's fixture data
14
     * @param $metadata the class metadata for doctrine
15
     * @param $embedded true for embedded documents
16
     * @return Object
17
     */
18
    public function createObject($class, $data, $metadata, $options = array())
19
    {
20
        // options to state if a document is to be embedded or persisted on its own
21
        $embedded = isset($options['embedded']);
22
        $mapping = array_keys($metadata->fieldMappings);
23
        // Instantiate new object
24
        $object = $this->makeInstance($class, $data);
25
        unset($data['__construct']);
26
27
        foreach ($data as $field => $value) {
28
            // Add the fields defined in the fixtures file
29
            $method = Inflector::camelize('set_' . $field);
30
            // This is a standard field
31
            if (in_array($field, $mapping)) {
32
                // Dates need to be converted to DateTime objects
33
                $type = $metadata->fieldMappings[$field]['type'];
34
                if ($type == 'many') {
35
                    $method = Inflector::camelize('add_'.$field);
36
                    // EmbedMany
37
                    if (
38
                        isset($metadata->fieldMappings[$field]['embedded']) &&
39
                        $metadata->fieldMappings[$field]['embedded']) {
40
                        foreach ($value as $embedded_value) {
41
                            $embed_class = $metadata->fieldMappings[$field]['targetDocument'];
42
                            $embed_data = $embedded_value;
43
                            $embed_meta = $this->getMetaDataForClass($embed_class);
44
                            $value = $this->createObject($embed_class, $embed_data, $embed_meta, array(
45
                                'embedded' => true));
46
                            $object->$method($value);
47
                        }
48
                    //ReferenceMany
49
                    } else {
50
                        foreach ($value as $reference_object) {
51
                            $object->$method($this->loader->getReference($reference_object));
52
                        }
53
                    }
54
                } else {
55 View Code Duplication
                    if ($type == 'datetime' || $type == 'date' || $type == 'time') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
                        $value = new \DateTime($value);
57
                    }
58
                    if ($type == 'one') {
59
                        // EmbedOne
60
                        if (
61
                            isset($metadata->fieldMappings[$field]['embedded']) &&
62
                            $metadata->fieldMappings[$field]['embedded']) {
63
                            $embed_class = $metadata->fieldMappings[$field]['targetDocument'];
64
                            $embed_data = $value;
65
                            $embed_meta = $this->getMetaDataForClass($embed_class);
66
                            $value = $this->createObject($embed_class, $embed_data, $embed_meta, array(
67
                                'embedded' => true));
68
                        // ReferenceOne
69
                        } else {
70
                            $value = $this->loader->getReference($value);
71
                        }
72
                    }
73
                    $object->$method($value);
74
                }
75
            } else {
76
                // The key is not a field's name but the name of a method to be called
77
                $object->$method($value);
78
            }
79
        }
80
81
        // Save a reference to the current object
82
        if (!$embedded) {
83
            $this->runServiceCalls($object);
84
        }
85
86
        return $object;
87
    }
88
}
89