Completed
Pull Request — master (#1716)
by Maciej
10:47
created

onlySetStrategyAllowedInShardKey()   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 2
crap 1
1
<?php
2
3
namespace Doctrine\ODM\MongoDB\Mapping;
4
5
use Doctrine\Common\Persistence\Mapping\MappingException as BaseMappingException;
6
7
/**
8
 * Class for all exceptions related to the Doctrine MongoDB ODM
9
 *
10
 * @since       1.0
11
 */
12
class MappingException extends BaseMappingException
13
{
14
    /**
15
     * @param string $name
16
     * @return MappingException
17
     */
18
    public static function typeExists($name)
19
    {
20
        return new self('Type ' . $name . ' already exists.');
21
    }
22
23
    /**
24
     * @param string $name
25
     * @return MappingException
26
     */
27
    public static function typeNotFound($name)
28
    {
29
        return new self('Type to be overwritten ' . $name . ' does not exist.');
30
    }
31
32
    /**
33
     * @param string $className
34
     * @param string $fieldName
35
     * @return MappingException
36
     */
37 6
    public static function mappingNotFound($className, $fieldName)
38
    {
39 6
        return new self("No mapping found for field '$fieldName' in class '$className'.");
40
    }
41
42
    /**
43
     * @param string $className
44
     * @param string $fieldName
45
     * @return MappingException
46
     */
47
    public static function referenceMappingNotFound($className, $fieldName)
48
    {
49
        return new self("No reference mapping found for field '$fieldName' in class '$className'.");
50
    }
51
52
    /**
53
     * @param string $className
54
     * @param string $fieldName
55
     * @return MappingException
56
     */
57 2
    public static function mappingNotFoundInClassNorDescendants($className, $fieldName)
58
    {
59 2
        return new self("No mapping found for field '$fieldName' in class '$className' nor its descendants.");
60
    }
61
62
    /**
63
     * @param $fieldName
64
     * @param $className
65
     * @param $className2
66
     * @return MappingException
67
     */
68 2
    public static function referenceFieldConflict($fieldName, $className, $className2)
69
    {
70 2
        return new self("Reference mapping for field '$fieldName' in class '$className' conflicts with one mapped in class '$className2'.");
71
    }
72
73
    /**
74
     * @param string $className
75
     * @param string $dbFieldName
76
     * @return MappingException
77
     */
78
    public static function mappingNotFoundByDbName($className, $dbFieldName)
79
    {
80
        return new self("No mapping found for field by DB name '$dbFieldName' in class '$className'.");
81
    }
82
83
    /**
84
     * @param string $document
85
     * @param string $fieldName
86
     * @return MappingException
87
     */
88
    public static function duplicateFieldMapping($document, $fieldName)
89
    {
90
        return new self('Property "' . $fieldName . '" in "' . $document . '" was already declared, but it must be declared only once');
91
    }
92
93
    /**
94
     * @param string $document
95
     * @param string $fieldName
96
     * @return MappingException
97
     */
98 2
    public static function discriminatorFieldConflict($document, $fieldName)
99
    {
100 2
        return new self('Discriminator field "' . $fieldName . '" in "' . $document . '" conflicts with a mapped field\'s "name" attribute.');
101
    }
102
103
    /**
104
     * Throws an exception that indicates that a class used in a discriminator map does not exist.
105
     * An example would be an outdated (maybe renamed) classname.
106
     *
107
     * @param string $className The class that could not be found
108
     * @param string $owningClass The class that declares the discriminator map.
109
     * @return MappingException
110
     */
111
    public static function invalidClassInDiscriminatorMap($className, $owningClass)
112
    {
113
        return new self(
114
            "Document class '$className' used in the discriminator map of class '$owningClass' " .
115
            'does not exist.'
116
        );
117
    }
118
119
    /**
120
     * Throws an exception that indicates a discriminator value does not exist in a map
121
     *
122
     * @param string $value The discriminator value that could not be found
123
     * @param string $owningClass The class that declares the discriminator map
124
     * @return MappingException
125
     */
126
    public static function invalidDiscriminatorValue($value, $owningClass)
127
    {
128
        return new self("Discriminator value '$value' used in the declaration of class '$owningClass' does not exist.");
129
    }
130
131
    /**
132
     * @param string $className
133
     * @return MappingException
134
     */
135
    public static function missingFieldName($className)
136
    {
137
        return new self("The Document class '$className' field mapping misses the 'fieldName' attribute.");
138
    }
139
140
    /**
141
     * @param string $className
142
     * @return MappingException
143
     */
144 3
    public static function classIsNotAValidDocument($className)
145
    {
146 3
        return new self('Class ' . $className . ' is not a valid document or mapped super class.');
147
    }
148
149
    /**
150
     * Exception for reflection exceptions - adds the document name,
151
     * because there might be long classnames that will be shortened
152
     * within the stacktrace
153
     *
154
     * @param string $document The document's name
155
     * @param \ReflectionException $previousException
156
     * @return \Doctrine\ODM\MongoDB\Mapping\MappingException
157
     */
158
    public static function reflectionFailure($document, \ReflectionException $previousException)
159
    {
160
        return new self('An error occurred in ' . $document, 0, $previousException);
161
    }
162
163
    /**
164
     * @param string $documentName
165
     * @return MappingException
166
     */
167
    public static function identifierRequired($documentName)
168
    {
169
        return new self("No identifier/primary key specified for Document '$documentName'."
170
            . ' Every Document must have an identifier/primary key.');
171
    }
172
173
    /**
174
     * @param string $className
175
     * @param string $fieldName
176
     * @return MappingException
177
     */
178
    public static function missingIdentifierField($className, $fieldName)
179
    {
180
        return new self("The identifier $fieldName is missing for a query of " . $className);
181
    }
182
183
    /**
184
     * @param string $className
185
     * @return MappingException
186
     */
187
    public static function missingIdGeneratorClass($className)
188
    {
189
        return new self("The class-option for the custom ID generator is missing in class $className.");
190
    }
191
192
    /**
193
     * @param string $className
194
     * @return MappingException
195
     */
196
    public static function classIsNotAValidGenerator($className)
197
    {
198
        return new self("The class $className if not a valid ID generator of type AbstractIdGenerator.");
199
    }
200
201
    /**
202
     * @param string $className
203
     * @param string $optionName
204
     * @return MappingException
205
     */
206
    public static function missingGeneratorSetter($className, $optionName)
207
    {
208
        return new self("The class $className is missing a setter for the option $optionName.");
209
    }
210
211
    /**
212
     * @param string $className
213
     * @param string $fieldName
214
     * @return MappingException
215
     */
216 1
    public static function cascadeOnEmbeddedNotAllowed($className, $fieldName)
217
    {
218 1
        return new self("Cascade on $className::$fieldName is not allowed.");
219
    }
220
221
    /**
222
     * @param string $className
223
     * @param string $fieldName
224
     * @return MappingException
225
     */
226 3
    public static function simpleReferenceRequiresTargetDocument($className, $fieldName)
227
    {
228 3
        return new self("Target document must be specified for simple reference: $className::$fieldName");
229
    }
230
231
    /**
232
     * @param string $targetDocument
233
     * @return MappingException
234
     */
235 1
    public static function simpleReferenceMustNotTargetDiscriminatedDocument($targetDocument)
236
    {
237 1
        return new self("Simple reference must not target document using Single Collection Inheritance, $targetDocument targeted.");
238
    }
239
240
    /**
241
     * @param string $strategy
242
     * @param string $className
243
     * @param string $fieldName
244
     * @return MappingException
245
     */
246 1
    public static function atomicCollectionStrategyNotAllowed($strategy, $className, $fieldName)
247
    {
248 1
        return new self("$strategy collection strategy can be used only in top level document, used in $className::$fieldName");
249
    }
250
251
    /**
252
     * @param string $className
253
     * @param string $fieldName
254
     * @return MappingException
255
     */
256 4
    public static function owningAndInverseReferencesRequireTargetDocument($className, $fieldName)
257
    {
258 4
        return new self("Target document must be specified for owning/inverse sides of reference: $className::$fieldName");
259
    }
260
261
    /**
262
     * @param string $className
263
     * @param string $fieldName
264
     * @return MappingException
265
     */
266 1
    public static function mustNotChangeIdentifierFieldsType($className, $fieldName)
267
    {
268 1
        return new self("$className::$fieldName was declared an identifier and must stay this way.");
269
    }
270
271
    /**
272
     * @param string $className
273
     * @param string $fieldName
274
     * @param string $strategy
275
     * @return MappingException
276
     */
277 1
    public static function referenceManySortMustNotBeUsedWithNonSetCollectionStrategy($className, $fieldName, $strategy)
278
    {
279 1
        return new self("ReferenceMany's sort can not be used with addToSet and pushAll strategies, $strategy used in $className::$fieldName");
280
    }
281
282
    /**
283
     * @param string $className
284
     * @param string $fieldName
285
     * @param string $type
286
     * @param string $strategy
287
     * @return MappingException
288
     */
289
    public static function invalidStorageStrategy($className, $fieldName, $type, $strategy)
290
    {
291
        return new self("Invalid strategy $strategy used in $className::$fieldName with type $type");
292
    }
293
294
    /**
295
     * @param string $className
296
     * @param string $fieldName
297
     * @param string $collectionClass
298
     * @return MappingException
299
     */
300 1
    public static function collectionClassDoesNotImplementCommonInterface($className, $fieldName, $collectionClass)
301
    {
302 1
        return new self("$collectionClass used as custom collection class for $className::$fieldName has to implement Doctrine\\Common\\Collections\\Collection interface.");
303
    }
304
305
    /**
306
     * @param $subclassName
307
     * @return MappingException
308
     */
309 2
    public static function shardKeyInSingleCollInheritanceSubclass($subclassName)
310
    {
311 2
        return new self("Shard key overriding in subclass is forbidden for single collection inheritance: $subclassName");
312
    }
313
314
    /**
315
     * @param $className
316
     * @return MappingException
317
     */
318 2
    public static function embeddedDocumentCantHaveShardKey($className)
319
    {
320 2
        return new self("Embedded document can't have shard key: $className");
321
    }
322
323
    /**
324
     * @param string $className
325
     * @param string $fieldName
326
     * @return MappingException
327
     */
328 1
    public static function onlySetStrategyAllowedInShardKey($className, $fieldName)
329
    {
330 1
        return new self("Only fields using the SET strategy can be used in the shard key: $className::$fieldName");
331
    }
332
333
    /**
334
     * @param $className
335
     * @param $fieldName
336
     * @return MappingException
337
     */
338 3
    public static function noMultiKeyShardKeys($className, $fieldName)
339
    {
340 3
        return new self("No multikey indexes are allowed in the shard key: $className::$fieldName");
341
    }
342
343
    /**
344
     * @param string $className
345
     * @param string $fieldName
346
     * @return MappingException
347
     */
348
    public static function cannotLookupDbRefReference($className, $fieldName)
349
    {
350
        return new self("Cannot use reference '$fieldName' in class '$className' for lookup or graphLookup: dbRef references are not supported.");
351
    }
352
353
    /**
354
     * @param string $className
355
     * @param string $fieldName
356
     * @return MappingException
357
     */
358
    public static function repositoryMethodLookupNotAllowed($className, $fieldName)
359
    {
360
        return new self("Cannot use reference '$fieldName' in class '$className' for lookup or graphLookup. repositoryMethod is not supported in \$lookup and \$graphLookup stages.");
361
    }
362
363
    /**
364
     * @param string $className
365
     * @return MappingException
366
     */
367 1
    public static function cannotUseShardedCollectionInOutStage($className)
368
    {
369 1
        return new self("Cannot use class '$className' as collection for out stage. Sharded collections are not allowed.");
370
    }
371
372
    /**
373
     * @param string $className
374
     * @return MappingException
375
     */
376 3
    public static function cannotUseShardedCollectionInLookupStages($className)
377
    {
378 3
        return new self("Cannot use class '$className' as collection for lookup or graphLookup stage. Sharded collections are not allowed.");
379
    }
380
381
    /**
382
     * @param string $className
383
     * @param string $fieldName
384
     *
385
     * @return MappingException
386
     */
387
    public static function referencePrimersOnlySupportedForInverseReferenceMany($className, $fieldName)
388
    {
389
        return new self("Cannot use reference priming on '$fieldName' in class '$className'. Reference priming is only supported for inverse references");
390
    }
391
392 1
    public static function connectFromFieldMustReferenceSameDocument($fieldName)
393
    {
394 1
        return new self("Cannot use field '$fieldName' as connectFromField in a \$graphLookup stage. Reference must target the document itself.");
395
    }
396
}
397