Completed
Push — master ( 707688...6da863 )
by Mike
04:47 queued 02:08
created

ODM/CouchDB/Mapping/Driver/AnnotationDriver.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\CouchDB\Mapping\Driver;
21
22
use Doctrine\Common\Annotations\AnnotationReader,
23
    Doctrine\Common\Annotations\AnnotationRegistry,
24
    Doctrine\Common\Annotations\Reader,
25
    Doctrine\Common\Persistence\Mapping\ClassMetadata,
26
    Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver as AbstractAnnotationDriver,
27
    Doctrine\ODM\CouchDB\Mapping\Annotations as ODM,
28
    Doctrine\ODM\CouchDB\Mapping\MappingException;
29
30
/**
31
 * The AnnotationDriver reads the mapping metadata from docblock annotations.
32
 *
33
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
34
 * @link        www.doctrine-project.org
35
 * @since       1.0
36
 * @author      Jonathan H. Wage <[email protected]>
37
 * @author      Roman Borschel <[email protected]>
38
 */
39
class AnnotationDriver extends AbstractAnnotationDriver
40
{
41
    protected $entityAnnotationClasses = array(
42
        'Doctrine\ODM\CouchDB\Mapping\Annotations\Document' => true,
43
        'Doctrine\ODM\CouchDB\Mapping\Annotations\EmbeddedDocument' => true,
44
        'Doctrine\ODM\CouchDB\Mapping\Annotations\MappedSuperclass' => true,
45
    );
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function loadMetadataForClass($className, ClassMetadata $class)
51
    {
52
        $reflClass = $class->getReflectionClass();
53
54
        $isValidDocument  = false;
55
        $classAnnotations = $this->reader->getClassAnnotations($reflClass);
56
57
        foreach ($classAnnotations AS $classAnnotation) {
58
            if ($classAnnotation instanceof ODM\Document) {
59
                if ($classAnnotation->indexed) {
60
                    $class->indexed = true;
61
                }
62
                $class->setCustomRepositoryClass($classAnnotation->repositoryClass);
63
                $isValidDocument = true;
64
            } elseif ($classAnnotation instanceof ODM\EmbeddedDocument) {
65
                $class->isEmbeddedDocument = true;
66
                $isValidDocument = true;
67
            } else if ($classAnnotation instanceof ODM\MappedSuperclass) {
68
                $class->isMappedSuperclass = true;
69
                $isValidDocument = true;
70
            } else if ($classAnnotation instanceof ODM\Index) {
71
                $class->indexed = true;
72
            } else if ($classAnnotation instanceof ODM\InheritanceRoot) {
73
                $class->markInheritanceRoot();
74
            }
75
        }
76
77
        if ( ! $isValidDocument) {
78
            throw MappingException::classIsNotAValidDocument($className);
79
        }
80
81
        foreach ($reflClass->getProperties() as $property) {
82
            if ($class->isInheritedAssociation($property->name) || $class->isInheritedField($property->name)) {
83
                continue;
84
            }
85
86
            $mapping = array();
87
            $mapping['fieldName'] = $property->name;
88
89
            if ($this->reader->getPropertyAnnotation($property, 'Doctrine\ODM\CouchDB\Mapping\Annotations\Index')) {
90
                $mapping['indexed'] = true;
91
            }
92
93
            foreach ($this->reader->getPropertyAnnotations($property) as $fieldAnnot) {
94
                if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Field) {
95
                    if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Version) {
96
                        $mapping['isVersionField'] = true;
97
                    }
98
99
                    $mapping = array_merge($mapping, (array) $fieldAnnot);
100
                    unset($mapping['value']);
101
                    $class->mapField($mapping);
102
                } else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\ReferenceOne) {
103
                    $mapping = array_merge($mapping, (array) $fieldAnnot);
104
                    $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade);
105
                    unset($mapping['value']);
106
                    $class->mapManyToOne($mapping);
107
                } else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\ReferenceMany) {
108
                    $mapping = array_merge($mapping, (array) $fieldAnnot);
109
                    $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade);
110
                    unset($mapping['value']);
111
                    $class->mapManyToMany($mapping);
112
                } else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Attachments) {
113
                    $class->mapAttachments($mapping['fieldName']);
114
                } else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\EmbedOne || $fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\EmbedMany) {
115
                    $mapping = array_merge($mapping, (array) $fieldAnnot);
116
                    unset($mapping['value']);
117
                    $class->mapEmbedded($mapping);
118
                }
119
            }
120
        }
121
    }
122
123
    /**
124
     * Gathers a list of cascade options found in the given cascade element.
125
     *
126
     * @param array $cascadeList cascade list
127
     * @return integer a bitmask of cascade options.
128
     * @throws MappingException
129
     */
130 View Code Duplication
    private function getCascadeMode(array $cascadeList)
0 ignored issues
show
This method seems to be duplicated in 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...
131
    {
132
        $cascade = 0;
133
        foreach ($cascadeList as $cascadeMode) {
134
            $constantName = 'Doctrine\ODM\CouchDB\Mapping\ClassMetadata::CASCADE_' . strtoupper($cascadeMode);
135
            if (!defined($constantName)) {
136
                throw new MappingException("Cascade mode '$cascadeMode' not supported.");
137
            }
138
            $cascade |= constant($constantName);
139
        }
140
        return $cascade;
141
    }
142
}
143