Completed
Push — master ( 2f1eb7...23b3b5 )
by Maciej
10s
created

XmlDriver::addEmbedMapping()   F

Complexity

Conditions 12
Paths 1024

Size

Total Lines 36
Code Lines 26

Duplication

Lines 10
Ratio 27.78 %

Code Coverage

Tests 30
CRAP Score 12.2341

Importance

Changes 0
Metric Value
dl 10
loc 36
ccs 30
cts 34
cp 0.8824
rs 2.7855
c 0
b 0
f 0
cc 12
eloc 26
nc 1024
nop 3
crap 12.2341

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
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ODM\MongoDB\Mapping\Driver;
21
22
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
23
use Doctrine\Common\Persistence\Mapping\Driver\FileDriver;
24
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as MappingClassMetadata;
25
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
26
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
27
28
/**
29
 * XmlDriver is a metadata driver that enables mapping through XML files.
30
 *
31
 * @since       1.0
32
 */
33
class XmlDriver extends FileDriver
34
{
35
    const DEFAULT_FILE_EXTENSION = '.dcm.xml';
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 14
    public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION)
41
    {
42 14
        parent::__construct($locator, $fileExtension);
43 14
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48 8
    public function loadMetadataForClass($className, ClassMetadata $class)
49
    {
50
        /* @var $class ClassMetadataInfo */
51
        /* @var $xmlRoot \SimpleXMLElement */
52 8
        $xmlRoot = $this->getElement($className);
53 8
        if ( ! $xmlRoot) {
54
            return;
55
        }
56
57 8
        if ($xmlRoot->getName() == 'document') {
58 8
            if (isset($xmlRoot['repository-class'])) {
59
                $class->setCustomRepositoryClass((string) $xmlRoot['repository-class']);
60
            }
61 8 View Code Duplication
        } elseif ($xmlRoot->getName() == 'mapped-superclass') {
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...
62 2
            $class->setCustomRepositoryClass(
63 2
                isset($xmlRoot['repository-class']) ? (string) $xmlRoot['repository-class'] : null
64 2
            );
65 2
            $class->isMappedSuperclass = true;
0 ignored issues
show
Bug introduced by
Accessing isMappedSuperclass on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
66 3
        } elseif ($xmlRoot->getName() == 'embedded-document') {
67 1
            $class->isEmbeddedDocument = true;
0 ignored issues
show
Bug introduced by
Accessing isEmbeddedDocument on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
68 1
        } elseif ($xmlRoot->getName() == 'query-result-document') {
69 1
            $class->isQueryResultDocument = true;
0 ignored issues
show
Bug introduced by
Accessing isQueryResultDocument on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
70 1
        }
71 8
        if (isset($xmlRoot['db'])) {
72 4
            $class->setDatabase((string) $xmlRoot['db']);
73 4
        }
74 8
        if (isset($xmlRoot['collection'])) {
75 7
            if (isset($xmlRoot['capped-collection'])) {
76
                $config = array('name' => (string) $xmlRoot['collection']);
77
                $config['capped'] = (bool) $xmlRoot['capped-collection'];
78
                if (isset($xmlRoot['capped-collection-max'])) {
79
                    $config['max'] = (int) $xmlRoot['capped-collection-max'];
80
                }
81
                if (isset($xmlRoot['capped-collection-size'])) {
82
                    $config['size'] = (int) $xmlRoot['capped-collection-size'];
83
                }
84
                $class->setCollection($config);
85
            } else {
86 7
                $class->setCollection((string) $xmlRoot['collection']);
87
            }
88 7
        }
89 8
        if (isset($xmlRoot['writeConcern'])) {
90 1
            $class->setWriteConcern((string) $xmlRoot['writeConcern']);
91 1
        }
92 8
        if (isset($xmlRoot['inheritance-type'])) {
93
            $inheritanceType = (string) $xmlRoot['inheritance-type'];
94
            $class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_' . $inheritanceType));
95
        }
96 8 View Code Duplication
        if (isset($xmlRoot['change-tracking-policy'])) {
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...
97 2
            $class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_' . strtoupper((string) $xmlRoot['change-tracking-policy'])));
98 2
        }
99 8
        if (isset($xmlRoot->{'discriminator-field'})) {
100 1
            $discrField = $xmlRoot->{'discriminator-field'};
101
            /* XSD only allows for "name", which is consistent with association
102
             * configurations, but fall back to "fieldName" for BC.
103
             */
104 1
            $class->setDiscriminatorField(
105 1
                isset($discrField['name']) ? (string) $discrField['name'] : (string) $discrField['fieldName']
106 1
            );
107 1
        }
108 8 View Code Duplication
        if (isset($xmlRoot->{'discriminator-map'})) {
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...
109 1
            $map = array();
110 1
            foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} AS $discrMapElement) {
111 1
                $map[(string) $discrMapElement['value']] = (string) $discrMapElement['class'];
112 1
            }
113 1
            $class->setDiscriminatorMap($map);
114 1
        }
115 8
        if (isset($xmlRoot->{'default-discriminator-value'})) {
116 1
            $class->setDefaultDiscriminatorValue((string) $xmlRoot->{'default-discriminator-value'}['value']);
117 1
        }
118 8
        if (isset($xmlRoot->{'indexes'})) {
119 3
            foreach ($xmlRoot->{'indexes'}->{'index'} as $index) {
120 3
                $this->addIndex($class, $index);
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
121 3
            }
122 3
        }
123 8
        if (isset($xmlRoot->{'shard-key'})) {
124 1
            $this->setShardKey($class, $xmlRoot->{'shard-key'}[0]);
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
125 1
        }
126 8
        if (isset($xmlRoot['require-indexes'])) {
127 1
            $class->setRequireIndexes('true' === (string) $xmlRoot['require-indexes']);
128 1
        }
129 8
        if (isset($xmlRoot['slave-okay'])) {
130 1
            $class->setSlaveOkay('true' === (string) $xmlRoot['slave-okay']);
131 1
        }
132 8
        if (isset($xmlRoot->field)) {
133 8
            foreach ($xmlRoot->field as $field) {
134 8
                $mapping = array();
135 8
                $attributes = $field->attributes();
136 8
                foreach ($attributes as $key => $value) {
137 8
                    $mapping[$key] = (string) $value;
138 8
                    $booleanAttributes = array('id', 'reference', 'embed', 'unique', 'sparse', 'file', 'distance');
139 8
                    if (in_array($key, $booleanAttributes)) {
140 8
                        $mapping[$key] = ('true' === $mapping[$key]);
141 8
                    }
142 8
                }
143 8
                if (isset($mapping['id']) && $mapping['id'] === true && isset($mapping['strategy'])) {
144 3
                    $mapping['options'] = array();
145 3
                    if (isset($field->{'id-generator-option'})) {
146 1
                        foreach ($field->{'id-generator-option'} as $generatorOptions) {
147 1
                            $attributesGenerator = iterator_to_array($generatorOptions->attributes());
148 1
                            if (isset($attributesGenerator['name']) && isset($attributesGenerator['value'])) {
149 1
                                $mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value'];
150 1
                            }
151 1
                        }
152 1
                    }
153 3
                }
154
155 8
                if (isset($attributes['not-saved'])) {
156
                    $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']);
157
                }
158
159 8
                if (isset($attributes['also-load'])) {
160
                    $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
161 8
                } elseif (isset($attributes['version'])) {
162 1
                    $mapping['version'] = ('true' === (string) $attributes['version']);
163 8
                } elseif (isset($attributes['lock'])) {
164 1
                    $mapping['lock'] = ('true' === (string) $attributes['lock']);
165 1
                }
166
167 8
                $this->addFieldMapping($class, $mapping);
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
168 8
            }
169 8
        }
170 8
        if (isset($xmlRoot->{'embed-one'})) {
171 2
            foreach ($xmlRoot->{'embed-one'} as $embed) {
172 2
                $this->addEmbedMapping($class, $embed, 'one');
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
173 2
            }
174 2
        }
175 8
        if (isset($xmlRoot->{'embed-many'})) {
176 2
            foreach ($xmlRoot->{'embed-many'} as $embed) {
177 2
                $this->addEmbedMapping($class, $embed, 'many');
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
178 2
            }
179 2
        }
180 8
        if (isset($xmlRoot->{'reference-many'})) {
181 3
            foreach ($xmlRoot->{'reference-many'} as $reference) {
182 3
                $this->addReferenceMapping($class, $reference, 'many');
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
183 3
            }
184 3
        }
185 8
        if (isset($xmlRoot->{'reference-one'})) {
186 3
            foreach ($xmlRoot->{'reference-one'} as $reference) {
187 3
                $this->addReferenceMapping($class, $reference, 'one');
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
188 3
            }
189 3
        }
190 8
        if (isset($xmlRoot->{'lifecycle-callbacks'})) {
191 2
            foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) {
192 2
                $class->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\ODM\MongoDB\Events::' . (string) $lifecycleCallback['type']));
193 2
            }
194 2
        }
195 8
        if (isset($xmlRoot->{'also-load-methods'})) {
196 1
            foreach ($xmlRoot->{'also-load-methods'}->{'also-load-method'} as $alsoLoadMethod) {
197 1
                $class->registerAlsoLoadMethod((string) $alsoLoadMethod['method'], (string) $alsoLoadMethod['field']);
198 1
            }
199 1
        }
200 8
    }
201
202 9
    private function addFieldMapping(ClassMetadataInfo $class, $mapping)
203
    {
204 9 View Code Duplication
        if (isset($mapping['name'])) {
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...
205 9
            $name = $mapping['name'];
206 9
        } elseif (isset($mapping['fieldName'])) {
207 1
            $name = $mapping['fieldName'];
208 1
        } else {
209
            throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping');
210
        }
211
212 9
        $class->mapField($mapping);
213
214
        // Index this field if either "index", "unique", or "sparse" are set
215 9 View Code Duplication
        if ( ! (isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) {
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...
216 9
            return;
217
        }
218
219 2
        $keys = array($name => isset($mapping['order']) ? $mapping['order'] : 'asc');
220 2
        $options = array();
221
222 2
        if (isset($mapping['background'])) {
223
            $options['background'] = (boolean) $mapping['background'];
224
        }
225 2
        if (isset($mapping['drop-dups'])) {
226 1
            $options['dropDups'] = (boolean) $mapping['drop-dups'];
227 1
        }
228 2
        if (isset($mapping['index-name'])) {
229
            $options['name'] = (string) $mapping['index-name'];
230
        }
231 2
        if (isset($mapping['safe'])) {
232
            $options['safe'] = (boolean) $mapping['safe'];
233
        }
234 2
        if (isset($mapping['sparse'])) {
235 1
            $options['sparse'] = (boolean) $mapping['sparse'];
236 1
        }
237 2
        if (isset($mapping['unique'])) {
238 2
            $options['unique'] = (boolean) $mapping['unique'];
239 2
        }
240
241 2
        $class->addIndex($keys, $options);
242 2
    }
243
244 2
    private function addEmbedMapping(ClassMetadataInfo $class, $embed, $type)
245
    {
246 2
        $attributes = $embed->attributes();
247 2
        $defaultStrategy = $type == 'one' ? ClassMetadataInfo::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
248
        $mapping = array(
249 2
            'type'            => $type,
250 2
            'embedded'        => true,
251 2
            'targetDocument'  => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null,
252 2
            'collectionClass' => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null,
253 2
            'name'            => (string) $attributes['field'],
254 2
            'strategy'        => isset($attributes['strategy']) ? (string) $attributes['strategy'] : $defaultStrategy,
255 2
        );
256 2
        if (isset($attributes['fieldName'])) {
257 1
            $mapping['fieldName'] = (string) $attributes['fieldName'];
258 1
        }
259 2 View Code Duplication
        if (isset($embed->{'discriminator-field'})) {
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...
260 1
            $attr = $embed->{'discriminator-field'};
261 1
            $mapping['discriminatorField'] = (string) $attr['name'];
262 1
        }
263 2 View Code Duplication
        if (isset($embed->{'discriminator-map'})) {
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...
264 1
            foreach ($embed->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) {
265 1
                $attr = $discriminatorMapping->attributes();
266 1
                $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class'];
267 1
            }
268 1
        }
269 2
        if (isset($embed->{'default-discriminator-value'})) {
270 1
            $mapping['defaultDiscriminatorValue'] = (string) $embed->{'default-discriminator-value'}['value'];
271 1
        }
272 2
        if (isset($attributes['not-saved'])) {
273
            $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']);
274
        }
275 2
        if (isset($attributes['also-load'])) {
276
            $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
277
        }
278 2
        $this->addFieldMapping($class, $mapping);
279 2
    }
280
281 4
    private function addReferenceMapping(ClassMetadataInfo $class, $reference, $type)
282
    {
283 4
        $cascade = array_keys((array) $reference->cascade);
284 4
        if (1 === count($cascade)) {
285 2
            $cascade = current($cascade) ?: next($cascade);
286 2
        }
287 4
        $attributes = $reference->attributes();
288 4
        $defaultStrategy = $type == 'one' ? ClassMetadataInfo::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
289
        $mapping = array(
290 4
            'cascade'          => $cascade,
291 4
            'orphanRemoval'    => isset($attributes['orphan-removal']) ? ('true' === (string) $attributes['orphan-removal']) : false,
292 4
            'type'             => $type,
293 4
            'reference'        => true,
294 4
            'simple'           => isset($attributes['simple']) ? ('true' === (string) $attributes['simple']) : false, // deprecated
295 4
            'storeAs'          => isset($attributes['store-as']) ? (string) $attributes['store-as'] : ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB,
296 4
            'targetDocument'   => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null,
297 4
            'collectionClass'  => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null,
298 4
            'name'             => (string) $attributes['field'],
299 4
            'strategy'         => isset($attributes['strategy']) ? (string) $attributes['strategy'] : $defaultStrategy,
300 4
            'inversedBy'       => isset($attributes['inversed-by']) ? (string) $attributes['inversed-by'] : null,
301 4
            'mappedBy'         => isset($attributes['mapped-by']) ? (string) $attributes['mapped-by'] : null,
302 4
            'repositoryMethod' => isset($attributes['repository-method']) ? (string) $attributes['repository-method'] : null,
303 4
            'limit'            => isset($attributes['limit']) ? (integer) $attributes['limit'] : null,
304 4
            'skip'             => isset($attributes['skip']) ? (integer) $attributes['skip'] : null,
305 4
        );
306
307 4
        if (isset($attributes['fieldName'])) {
308 1
            $mapping['fieldName'] = (string) $attributes['fieldName'];
309 1
        }
310 4 View Code Duplication
        if (isset($reference->{'discriminator-field'})) {
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...
311 1
            $attr = $reference->{'discriminator-field'};
312 1
            $mapping['discriminatorField'] = (string) $attr['name'];
313 1
        }
314 4 View Code Duplication
        if (isset($reference->{'discriminator-map'})) {
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...
315 1
            foreach ($reference->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) {
316 1
                $attr = $discriminatorMapping->attributes();
317 1
                $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class'];
318 1
            }
319 1
        }
320 4
        if (isset($reference->{'default-discriminator-value'})) {
321 1
            $mapping['defaultDiscriminatorValue'] = (string) $reference->{'default-discriminator-value'}['value'];
322 1
        }
323 4
        if (isset($reference->{'sort'})) {
324 View Code Duplication
            foreach ($reference->{'sort'}->{'sort'} as $sort) {
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...
325
                $attr = $sort->attributes();
326
                $mapping['sort'][(string) $attr['field']] = isset($attr['order']) ? (string) $attr['order'] : 'asc';
327
            }
328
        }
329 4
        if (isset($reference->{'criteria'})) {
330 View Code Duplication
            foreach ($reference->{'criteria'}->{'criteria'} as $criteria) {
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...
331
                $attr = $criteria->attributes();
332
                $mapping['criteria'][(string) $attr['field']] = (string) $attr['value'];
333
            }
334
        }
335 4
        if (isset($attributes['not-saved'])) {
336
            $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']);
337
        }
338 4
        if (isset($attributes['also-load'])) {
339
            $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
340
        }
341 4
        $this->addFieldMapping($class, $mapping);
342 4
    }
343
344 3
    private function addIndex(ClassMetadataInfo $class, \SimpleXmlElement $xmlIndex)
345
    {
346 3
        $attributes = $xmlIndex->attributes();
347
348 3
        $keys = array();
349
350 3 View Code Duplication
        foreach ($xmlIndex->{'key'} as $key) {
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...
351 3
            $keys[(string) $key['name']] = isset($key['order']) ? (string) $key['order'] : 'asc';
352 3
        }
353
354 3
        $options = array();
355
356 3
        if (isset($attributes['background'])) {
357
            $options['background'] = ('true' === (string) $attributes['background']);
358
        }
359 3
        if (isset($attributes['drop-dups'])) {
360
            $options['dropDups'] = ('true' === (string) $attributes['drop-dups']);
361
        }
362 3
        if (isset($attributes['name'])) {
363
            $options['name'] = (string) $attributes['name'];
364
        }
365 3
        if (isset($attributes['safe'])) {
366
            $options['safe'] = ('true' === (string) $attributes['safe']);
367
        }
368 3
        if (isset($attributes['sparse'])) {
369
            $options['sparse'] = ('true' === (string) $attributes['sparse']);
370
        }
371 3
        if (isset($attributes['unique'])) {
372 1
            $options['unique'] = ('true' === (string) $attributes['unique']);
373 1
        }
374
375 3 View Code Duplication
        if (isset($xmlIndex->{'option'})) {
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...
376 1
            foreach ($xmlIndex->{'option'} as $option) {
377 1
                $value = (string) $option['value'];
378 1
                if ($value === 'true') {
379
                    $value = true;
380 1
                } elseif ($value === 'false') {
381 1
                    $value = false;
382 1
                } elseif (is_numeric($value)) {
383 1
                    $value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value;
384 1
                }
385 1
                $options[(string) $option['name']] = $value;
386 1
            }
387 1
        }
388
389 3
        if (isset($xmlIndex->{'partial-filter-expression'})) {
390 3
            $partialFilterExpressionMapping = $xmlIndex->{'partial-filter-expression'};
391
392 3
            if (isset($partialFilterExpressionMapping->and)) {
393 2
                foreach ($partialFilterExpressionMapping->and as $and) {
394 2
                    if (! isset($and->field)) {
395 1
                        continue;
396
                    }
397
398 2
                    $partialFilterExpression = $this->getPartialFilterExpression($and->field);
399 2
                    if (! $partialFilterExpression) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $partialFilterExpression 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...
400
                        continue;
401
                    }
402
403 2
                    $options['partialFilterExpression']['$and'][] = $partialFilterExpression;
404 2
                }
405 3
            } elseif (isset($partialFilterExpressionMapping->field)) {
406 2
                $partialFilterExpression = $this->getPartialFilterExpression($partialFilterExpressionMapping->field);
407
408 2
                if ($partialFilterExpression) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $partialFilterExpression 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...
409 2
                    $options['partialFilterExpression'] = $partialFilterExpression;
410 2
                }
411 2
            }
412 3
        }
413
414 3
        $class->addIndex($keys, $options);
415 3
    }
416
417 3
    private function getPartialFilterExpression(\SimpleXMLElement $fields)
418
    {
419 3
        $partialFilterExpression = [];
420 3
        foreach ($fields as $field) {
421 3
            $operator = (string) $field['operator'] ?: null;
422
423 3
            if (! isset($field['value'])) {
424 1
                if (! isset($field->field)) {
425
                    continue;
426
                }
427
428 1
                $nestedExpression = $this->getPartialFilterExpression($field->field);
429 1
                if (! $nestedExpression) {
430
                    continue;
431
                }
432
433 1
                $value = $nestedExpression;
434 1
            } else {
435 3
                $value = trim((string) $field['value']);
436
            }
437
438 3
            if ($value === 'true') {
439
                $value = true;
440 3
            } elseif ($value === 'false') {
441
                $value = false;
442 3
            } elseif (is_numeric($value)) {
443 2
                $value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value;
444 2
            }
445
446 3
            $partialFilterExpression[(string) $field['name']] = $operator ? ['$' . $operator => $value] : $value;
447 3
        }
448
449 3
        return $partialFilterExpression;
450
    }
451
452 2
    private function setShardKey(ClassMetadataInfo $class, \SimpleXmlElement $xmlShardkey)
453
    {
454 2
        $attributes = $xmlShardkey->attributes();
455
456 2
        $keys = array();
457 2
        $options = array();
458 2 View Code Duplication
        foreach ($xmlShardkey->{'key'} as $key) {
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...
459 2
            $keys[(string) $key['name']] = isset($key['order']) ? (string)$key['order'] : 'asc';
460 2
        }
461
462 2
        if (isset($attributes['unique'])) {
463 1
            $options['unique'] = ('true' === (string) $attributes['unique']);
464 1
        }
465
466 2
        if (isset($attributes['numInitialChunks'])) {
467 1
            $options['numInitialChunks'] = (int) $attributes['numInitialChunks'];
468 1
        }
469
470 2 View Code Duplication
        if (isset($xmlShardkey->{'option'})) {
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...
471 1
            foreach ($xmlShardkey->{'option'} as $option) {
472 1
                $value = (string) $option['value'];
473 1
                if ($value === 'true') {
474 1
                    $value = true;
475 1
                } elseif ($value === 'false') {
476
                    $value = false;
477 1
                } elseif (is_numeric($value)) {
478 1
                    $value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value;
479 1
                }
480 1
                $options[(string) $option['name']] = $value;
481 1
            }
482 1
        }
483
484 2
        $class->setShardKey($keys, $options);
485 2
    }
486
487
    /**
488
     * {@inheritDoc}
489
     */
490 8
    protected function loadMappingFile($file)
491
    {
492 8
        $result = array();
493 8
        $xmlElement = simplexml_load_file($file);
494
495 8
        foreach (array('document', 'embedded-document', 'mapped-superclass', 'query-result-document') as $type) {
496 8
            if (isset($xmlElement->$type)) {
497 8
                foreach ($xmlElement->$type as $documentElement) {
498 8
                    $documentName = (string) $documentElement['name'];
499 8
                    $result[$documentName] = $documentElement;
500 8
                }
501 8
            }
502 8
        }
503
504 8
        return $result;
505
    }
506
}
507