Completed
Pull Request — master (#1668)
by Andreas
04:58
created

MongoDBException::invalidDocumentState()   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
 * 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
     * @return MongoDBException
104
     */
105
    public static function invalidDocumentRepository($className)
106
    {
107
        return new self("Invalid repository class '".$className."'. It must be a Doctrine\Common\Persistence\ObjectRepository.");
108
    }
109
110
    /**
111
     * @param string $type
112
     * @param string|array $expected
113
     * @param mixed $got
114
     * @return MongoDBException
115
     */
116 4
    public static function invalidValueForType($type, $expected, $got)
117
    {
118 4
        if (is_array($expected)) {
119 4
            $expected = sprintf('%s or %s',
120 4
                join(', ', array_slice($expected, 0, -1)),
121 4
                end($expected)
122
            );
123
        }
124 4
        if (is_object($got)) {
125 2
            $gotType = get_class($got);
126 2
        } elseif (is_array($got)) {
127
            $gotType = 'array';
128
        } else {
129 2
            $gotType = 'scalar';
130
        }
131 4
        return new self(sprintf('%s type requires value of type %s, %s given', $type, $expected, $gotType));
132
    }
133
134
    /**
135
     * @param string $field
136
     * @param string $className
137
     * @return MongoDBException
138
     */
139 2
    public static function shardKeyFieldCannotBeChanged($field, $className)
140
    {
141 2
        return new self(sprintf('Shard key field "%s" in class "%s" cannot be changed.', $field, $className));
142
    }
143
144
    /**
145
     * @param string $field
146
     * @param string $className
147
     * @return MongoDBException
148
     */
149
    public static function shardKeyFieldMissing($field, $className)
150
    {
151
        return new self(sprintf('Shard key field "%s" in class "%s" is missing.', $field, $className));
152
    }
153
154
    /**
155
     * @param string $dbName
156
     * @param string $errorMessage
157
     * @return MongoDBException
158
     */
159 1
    public static function failedToEnableSharding($dbName, $errorMessage)
160
    {
161 1
        return new self(sprintf('Failed to enable sharding for database "%s". Error from MongoDB: %s',
162 1
            $dbName,
163 1
            $errorMessage
164
        ));
165
    }
166
167
    /**
168
     * @param string $className
169
     * @param string $errorMessage
170
     * @return MongoDBException
171
     */
172 1
    public static function failedToEnsureDocumentSharding($className, $errorMessage)
173
    {
174 1
        return new self(sprintf('Failed to ensure sharding for document "%s". Error from MongoDB: %s',
175 1
            $className,
176 1
            $errorMessage
177
        ));
178
    }
179
}
180