Completed
Push — master ( 73492b...2db3cf )
by Jeremy
11:56
created

MongoDBException   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 56.41%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 168
ccs 22
cts 39
cp 0.5641
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A invalidFindByCall() 0 4 1
A detachedDocumentCannotBeRemoved() 0 4 1
A invalidDocumentState() 0 4 1
A documentNotMappedToCollection() 0 4 1
A documentManagerClosed() 0 4 1
A findByRequiresParameter() 0 4 1
A unknownDocumentNamespace() 0 4 1
A cannotPersistMappedSuperclass() 0 4 1
A queryNotIndexed() 0 7 1
A invalidDocumentRepository() 0 4 1
A invalidValueForType() 0 17 4
A shardKeyFieldCannotBeChanged() 0 4 1
A shardKeyFieldMissing() 0 4 1
A failedToEnableSharding() 0 7 1
A failedToEnsureDocumentSharding() 0 7 1
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;
21
22
/**
23
 * Class for all exceptions related to the Doctrine MongoDB ODM
24
 *
25
 * @since       1.0
26
 */
27
class MongoDBException extends \Exception
28
{
29
    /**
30
     * @param string $documentName
31
     * @param string $fieldName
32
     * @param string $method
33
     * @return MongoDBException
34
     */
35
    public static function invalidFindByCall($documentName, $fieldName, $method)
36
    {
37
        return new self(sprintf('Invalid find by call %s::$fieldName (%s)', $documentName, $fieldName, $method));
38
    }
39
40
    /**
41
     * @return MongoDBException
42
     */
43
    public static function detachedDocumentCannotBeRemoved()
44
    {
45
        return new self('Detached document cannot be removed');
46
    }
47
48
    /**
49
     * @param string $state
50
     * @return MongoDBException
51
     */
52
    public static function invalidDocumentState($state)
53
    {
54
        return new self(sprintf('Invalid document state "%s"', $state));
55
    }
56
57
    /**
58
     * @param string $className
59
     * @return MongoDBException
60
     */
61
    public static function documentNotMappedToCollection($className)
62
    {
63
        return new self(sprintf('The "%s" document is not mapped to a MongoDB database collection.', $className));
64
    }
65
66
    /**
67
     * @return MongoDBException
68
     */
69 5
    public static function documentManagerClosed()
70
    {
71 5
        return new self('The DocumentManager is closed.');
72
    }
73
74
    /**
75
     * @param string $methodName
76
     * @return MongoDBException
77
     */
78
    public static function findByRequiresParameter($methodName)
79
    {
80
        return new self("You need to pass a parameter to '".$methodName."'");
81
    }
82
83
    /**
84
     * @param string $documentNamespaceAlias
85
     * @return MongoDBException
86
     */
87
    public static function unknownDocumentNamespace($documentNamespaceAlias)
88
    {
89
        return new self("Unknown Document namespace alias '$documentNamespaceAlias'.");
90
    }
91
92
    /**
93
     * @param string $className
94
     * @return MongoDBException
95
     */
96 1
    public static function cannotPersistMappedSuperclass($className)
97
    {
98 1
        return new self('Cannot persist an embedded document, aggregation result document or mapped superclass ' . $className);
99
    }
100
101
    /**
102
     * @param string $className
103
     * @param string $unindexedFields
104
     * @return MongoDBException
105
     *
106
     * @deprecated method was deprecated in 1.2 and will be removed in 2.0
107
     */
108 5
    public static function queryNotIndexed($className, $unindexedFields)
109
    {
110 5
        return new self(sprintf('Cannot execute unindexed queries on %s. Unindexed fields: %s',
111
            $className,
112 5
            implode(', ', $unindexedFields)
113
        ));
114
    }
115
116
    /**
117
     * @param string $className
118
     * @return MongoDBException
119
     */
120
    public static function invalidDocumentRepository($className)
121
    {
122
        return new self("Invalid repository class '".$className."'. It must be a Doctrine\Common\Persistence\ObjectRepository.");
123
    }
124
125
    /**
126
     * @param string $type
127
     * @param string|array $expected
128
     * @param mixed $got
129
     * @return MongoDBException
130
     */
131 4
    public static function invalidValueForType($type, $expected, $got)
132
    {
133 4
        if (is_array($expected)) {
134 4
            $expected = sprintf('%s or %s',
135 4
                join(', ', array_slice($expected, 0, -1)),
136
                end($expected)
137
            );
138
        }
139 4
        if (is_object($got)) {
140 2
            $gotType = get_class($got);
141 2
        } elseif (is_array($got)) {
142
            $gotType = 'array';
143
        } else {
144 2
            $gotType = 'scalar';
145
        }
146 4
        return new self(sprintf('%s type requires value of type %s, %s given', $type, $expected, $gotType));
147
    }
148
149
    /**
150
     * @param string $field
151
     * @param string $className
152
     * @return MongoDBException
153
     */
154 2
    public static function shardKeyFieldCannotBeChanged($field, $className)
155
    {
156 2
        return new self(sprintf('Shard key field "%s" in class "%s" cannot be changed.', $field, $className));
157
    }
158
159
    /**
160
     * @param string $field
161
     * @param string $className
162
     * @return MongoDBException
163
     */
164
    public static function shardKeyFieldMissing($field, $className)
165
    {
166
        return new self(sprintf('Shard key field "%s" in class "%s" is missing.', $field, $className));
167
    }
168
169
    /**
170
     * @param string $dbName
171
     * @param string $errorMessage
172
     * @return MongoDBException
173
     */
174 1
    public static function failedToEnableSharding($dbName, $errorMessage)
175
    {
176 1
        return new self(sprintf('Failed to enable sharding for database "%s". Error from MongoDB: %s',
177
            $dbName,
178
            $errorMessage
179
        ));
180
    }
181
182
    /**
183
     * @param string $className
184
     * @param string $errorMessage
185
     * @return MongoDBException
186
     */
187 1
    public static function failedToEnsureDocumentSharding($className, $errorMessage)
188
    {
189 1
        return new self(sprintf('Failed to ensure sharding for document "%s". Error from MongoDB: %s',
190
            $className,
191
            $errorMessage
192
        ));
193
    }
194
}
195