Completed
Push — master ( fc8c9f...e4147a )
by Jonathan
04:42 queued 02:05
created

ClassMetadataFactory::hasMetadataFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license. For more information, see
18
 * <http://www.doctrine-project.org>.
19
 */
20
21
namespace Doctrine\SkeletonMapper\Mapping;
22
23
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory as BaseClassMetadataFactory;
24
25
/**
26
 * Class responsible for retrieving ClassMetadata instances.
27
 *
28
 * @author Jonathan H. Wage <[email protected]>
29
 */
30
class ClassMetadataFactory implements BaseClassMetadataFactory
31
{
32
    /**
33
     * @var ClassMetadataInstantiatorInterface
34
     */
35
    private $classMetadataInstantiator;
36
37
    /**
38
     * @var array
39
     */
40
    protected $classes = array();
41
42
    /**
43
     * @param ClassMetadataInstantiatorInterface $classMetadataInstantiator
44
     */
45 89
    public function __construct(ClassMetadataInstantiatorInterface $classMetadataInstantiator)
46
    {
47 89
        $this->classMetadataInstantiator = $classMetadataInstantiator;
48 89
    }
49
50
    /**
51
     * Returns all mapped classes.
52
     *
53
     * @return array The ClassMetadata instances of all mapped classes.
54
     */
55
    public function getAllMetadata()
56
    {
57
        return $this->classes;
58
    }
59
60
    /**
61
     * Gets the class metadata descriptor for a class.
62
     *
63
     * @param string $className The name of the class.
64
     *
65
     * @return ClassMetadata
66
     */
67 89
    public function getMetadataFor($className)
68
    {
69 89
        if (!isset($this->classes[$className])) {
70 89
            $metadata = $this->classMetadataInstantiator->instantiate($className);
71
72 89
            if ($metadata->reflClass->implementsInterface('Doctrine\SkeletonMapper\Mapping\LoadMetadataInterface')) {
73 89
                $className::loadMetadata($metadata);
74 89
            }
75
76 89
            $this->classes[$className] = $metadata;
77 89
        }
78
79 89
        return $this->classes[$className];
80
    }
81
82
    /**
83
     * Checks whether the factory has the metadata for a class loaded already.
84
     *
85
     * @param string $className
86
     *
87
     * @return bool TRUE if the metadata of the class in question is already loaded, FALSE otherwise.
88
     */
89
    public function hasMetadataFor($className)
90
    {
91
        return isset($this->classes[$className]);
92
    }
93
94
    /**
95
     * Sets the metadata descriptor for a specific class.
96
     *
97
     * @param string        $className
98
     * @param ClassMetadata $class
99
     */
100 89
    public function setMetadataFor($className, $class)
101
    {
102 89
        $this->classes[$className] = $class;
103 89
    }
104
105
    /**
106
     * Returns whether the class with the specified name should have its metadata loaded.
107
     * This is only the case if it is either mapped directly or as a MappedSuperclass.
108
     *
109
     * @param string $className
110
     *
111
     * @return bool
112
     */
113
    public function isTransient($className)
114
    {
115
        return isset($this->classes[$className]);
116
    }
117
}
118