Completed
Pull Request — master (#1419)
by Robert
11:09 queued 01:09
created

MongoDBException::documentManagerClosed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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 or mapped superclass ' . $className);
99
    }
100
101
    /**
102
     * @param string $className
103
     * @param string $unindexedFields
104
     * @return MongoDBException
105
     */
106 5
    public static function queryNotIndexed($className, $unindexedFields)
107
    {
108 5
        return new self(sprintf('Cannot execute unindexed queries on %s. Unindexed fields: %s',
109
            $className,
110 5
            implode(', ', $unindexedFields)
111
        ));
112
    }
113
114
    /**
115
     * @param string $className
116
     * @return MongoDBException
117
     */
118
    public static function invalidDocumentRepository($className)
119
    {
120
        return new self("Invalid repository class '".$className."'. It must be a Doctrine\Common\Persistence\ObjectRepository.");
121
    }
122
123
    /**
124
     * @param string $type
125
     * @param string|array $expected
126
     * @param mixed $got
127
     * @return MongoDBException
128
     */
129 4
    public static function invalidValueForType($type, $expected, $got)
130
    {
131 4
        if (is_array($expected)) {
132 4
            $expected = sprintf('%s or %s',
133 4
                join(', ', array_slice($expected, 0, -1)),
134
                end($expected)
135
            );
136
        }
137 4
        if (is_object($got)) {
138 2
            $gotType = get_class($got);
139 2
        } elseif (is_array($got)) {
140
            $gotType = 'array';
141
        } else {
142 2
            $gotType = 'scalar';
143
        }
144 4
        return new self(sprintf('%s type requires value of type %s, %s given', $type, $expected, $gotType));
145
    }
146
147
    /**
148
     * @param string $field
149
     * @param string $className
150
     * @return MongoDBException
151
     */
152 2
    public static function shardKeyFieldCannotBeChanged($field, $className)
153
    {
154 2
        return new self(sprintf('Shard key field "%s" in class "%s" cannot be changed.', $field, $className));
155
    }
156
157
    /**
158
     * @param string $field
159
     * @param string $className
160
     * @return MongoDBException
161
     */
162
    public static function shardKeyFieldMissing($field, $className)
163
    {
164
        return new self(sprintf('Shard key field "%s" in class "%s" is missing.', $field, $className));
165
    }
166
167
    /**
168
     * @param string $dbName
169
     * @param string $errorMessage
170
     * @return MongoDBException
171
     */
172 1
    public static function failedToEnableSharding($dbName, $errorMessage)
173
    {
174 1
        return new self(sprintf('Failed to enable sharding for database "%s". Error from MongoDB: %s',
175
            $dbName,
176
            $errorMessage
177
        ));
178
    }
179
180
    /**
181
     * @param string $className
182
     * @param string $errorMessage
183
     * @return MongoDBException
184
     */
185 1
    public static function failedToEnsureDocumentSharding($className, $errorMessage)
186
    {
187 1
        return new self(sprintf('Failed to ensure sharding for document "%s". Error from MongoDB: %s',
188
            $className,
189
            $errorMessage
190
        ));
191
    }
192
}
193