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