1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ODM\MongoDB; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
8
|
|
|
use Doctrine\ODM\MongoDB\Repository\GridFSRepository; |
9
|
|
|
use function array_slice; |
10
|
|
|
use function end; |
11
|
|
|
use function get_class; |
12
|
|
|
use function implode; |
13
|
|
|
use function is_array; |
14
|
|
|
use function is_object; |
15
|
|
|
use function sprintf; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class for all exceptions related to the Doctrine MongoDB ODM |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
class MongoDBException extends \Exception |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @return MongoDBException |
25
|
|
|
*/ |
26
|
|
|
public static function detachedDocumentCannotBeRemoved() |
27
|
|
|
{ |
28
|
|
|
return new self('Detached document cannot be removed'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $state |
33
|
|
|
* @return MongoDBException |
34
|
|
|
*/ |
35
|
|
|
public static function invalidDocumentState($state) |
36
|
|
|
{ |
37
|
|
|
return new self(sprintf('Invalid document state "%s"', $state)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $className |
42
|
|
|
* @return MongoDBException |
43
|
|
|
*/ |
44
|
|
|
public static function documentNotMappedToCollection($className) |
45
|
|
|
{ |
46
|
|
|
return new self(sprintf('The "%s" document is not mapped to a MongoDB database collection.', $className)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return MongoDBException |
51
|
|
|
*/ |
52
|
5 |
|
public static function documentManagerClosed() |
53
|
|
|
{ |
54
|
5 |
|
return new self('The DocumentManager is closed.'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $documentNamespaceAlias |
59
|
|
|
* @return MongoDBException |
60
|
|
|
*/ |
61
|
|
|
public static function unknownDocumentNamespace($documentNamespaceAlias) |
62
|
|
|
{ |
63
|
|
|
return new self(sprintf("Unknown Document namespace alias '%s'.", $documentNamespaceAlias)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $className |
68
|
|
|
* @return MongoDBException |
69
|
|
|
*/ |
70
|
1 |
|
public static function cannotPersistMappedSuperclass($className) |
71
|
|
|
{ |
72
|
1 |
|
return new self('Cannot persist an embedded document, aggregation result document or mapped superclass ' . $className); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $className |
77
|
|
|
* @return MongoDBException |
78
|
|
|
*/ |
79
|
|
|
public static function invalidDocumentRepository($className) |
80
|
|
|
{ |
81
|
|
|
return new self(sprintf("Invalid repository class '%s'. It must be a %s.", $className, ObjectRepository::class)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $className |
86
|
|
|
* @return MongoDBException |
87
|
|
|
*/ |
88
|
|
|
public static function invalidGridFSRepository($className) |
89
|
|
|
{ |
90
|
|
|
return new self(sprintf("Invalid repository class '%s'. It must be a %s.", $className, GridFSRepository::class)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $type |
95
|
|
|
* @param string|array $expected |
96
|
|
|
* @param mixed $got |
97
|
|
|
* @return MongoDBException |
98
|
|
|
*/ |
99
|
4 |
|
public static function invalidValueForType($type, $expected, $got) |
100
|
|
|
{ |
101
|
4 |
|
if (is_array($expected)) { |
102
|
4 |
|
$expected = sprintf( |
103
|
4 |
|
'%s or %s', |
104
|
4 |
|
implode(', ', array_slice($expected, 0, -1)), |
105
|
4 |
|
end($expected) |
106
|
|
|
); |
107
|
|
|
} |
108
|
4 |
|
if (is_object($got)) { |
109
|
2 |
|
$gotType = get_class($got); |
110
|
2 |
|
} elseif (is_array($got)) { |
111
|
|
|
$gotType = 'array'; |
112
|
|
|
} else { |
113
|
2 |
|
$gotType = 'scalar'; |
114
|
|
|
} |
115
|
4 |
|
return new self(sprintf('%s type requires value of type %s, %s given', $type, $expected, $gotType)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $field |
120
|
|
|
* @param string $className |
121
|
|
|
* @return MongoDBException |
122
|
|
|
*/ |
123
|
|
|
public static function shardKeyFieldCannotBeChanged($field, $className) |
124
|
|
|
{ |
125
|
|
|
return new self(sprintf('Shard key field "%s" in class "%s" cannot be changed.', $field, $className)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $field |
130
|
|
|
* @param string $className |
131
|
|
|
* @return MongoDBException |
132
|
|
|
*/ |
133
|
|
|
public static function shardKeyFieldMissing($field, $className) |
134
|
|
|
{ |
135
|
|
|
return new self(sprintf('Shard key field "%s" in class "%s" is missing.', $field, $className)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $dbName |
140
|
|
|
* @param string $errorMessage |
141
|
|
|
* @return MongoDBException |
142
|
|
|
*/ |
143
|
|
|
public static function failedToEnableSharding($dbName, $errorMessage) |
144
|
|
|
{ |
145
|
|
|
return new self(sprintf( |
146
|
|
|
'Failed to enable sharding for database "%s". Error from MongoDB: %s', |
147
|
|
|
$dbName, |
148
|
|
|
$errorMessage |
149
|
|
|
)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $className |
154
|
|
|
* @param string $errorMessage |
155
|
|
|
* @return MongoDBException |
156
|
|
|
*/ |
157
|
|
|
public static function failedToEnsureDocumentSharding($className, $errorMessage) |
158
|
|
|
{ |
159
|
|
|
return new self(sprintf( |
160
|
|
|
'Failed to ensure sharding for document "%s". Error from MongoDB: %s', |
161
|
|
|
$className, |
162
|
|
|
$errorMessage |
163
|
|
|
)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return MongoDBException |
168
|
|
|
*/ |
169
|
|
|
public static function commitInProgress() |
170
|
|
|
{ |
171
|
|
|
return new self('There is already a commit operation in progress. Did you call flush from an event listener?'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public static function documentBucketOnlyAvailableForGridFSFiles(string $className): self |
175
|
|
|
{ |
176
|
|
|
return new self(sprintf('Cannot fetch document bucket for document "%s".', $className)); |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
public static function cannotPersistGridFSFile(string $className): self |
180
|
|
|
{ |
181
|
1 |
|
return new self(sprintf('Cannot persist GridFS file for class "%s" through UnitOfWork.', $className)); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public static function cannotReadGridFSSourceFile(string $filename): self |
185
|
|
|
{ |
186
|
|
|
return new self(sprintf('Cannot open file "%s" for uploading to GridFS.', $filename)); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|