Completed
Push — master ( ecb96f...9f19a4 )
by Andreas
21s queued 13s
created

MappingException::invalidRepositoryClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Mapping;
6
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\ODM\MongoDB\Mapping\Annotations\AbstractDocument;
9
use Doctrine\Persistence\Mapping\MappingException as BaseMappingException;
10
use ReflectionException;
11
use ReflectionObject;
12
use function sprintf;
13
14
/**
15
 * Class for all exceptions related to the Doctrine MongoDB ODM
16
 */
17
final class MappingException extends BaseMappingException
18
{
19
    public static function typeExists(string $name) : self
20
    {
21
        return new self(sprintf('Type %s already exists.', $name));
22
    }
23
24
    public static function typeNotFound(string $name) : self
25
    {
26
        return new self(sprintf('Type to be overwritten %s does not exist.', $name));
27
    }
28
29 6
    public static function mappingNotFound(string $className, string $fieldName) : self
30
    {
31 6
        return new self(sprintf("No mapping found for field '%s' in class '%s'.", $fieldName, $className));
32
    }
33
34
    public static function referenceMappingNotFound(string $className, string $fieldName) : self
35
    {
36
        return new self(sprintf("No reference mapping found for field '%s' in class '%s'.", $fieldName, $className));
37
    }
38
39 2
    public static function mappingNotFoundInClassNorDescendants(string $className, string $fieldName) : self
40
    {
41 2
        return new self(sprintf("No mapping found for field '%s' in class '%s' nor its descendants.", $fieldName, $className));
42
    }
43
44 2
    public static function referenceFieldConflict(string $fieldName, string $className, string $className2) : self
45
    {
46 2
        return new self(sprintf("Reference mapping for field '%s' in class '%s' conflicts with one mapped in class '%s'.", $fieldName, $className, $className2));
47
    }
48
49 2
    public static function mappingNotFoundByDbName(string $className, string $dbFieldName) : self
50
    {
51 2
        return new self(sprintf("No mapping found for field by DB name '%s' in class '%s'.", $dbFieldName, $className));
52
    }
53
54
    public static function duplicateFieldMapping(string $document, string $fieldName) : self
55
    {
56
        return new self(sprintf('Property "%s" in "%s" was already declared, but it must be declared only once', $fieldName, $document));
57
    }
58
59 2
    public static function duplicateDatabaseFieldName(string $document, string $offendingFieldName, string $databaseName, string $originalFieldName) : self
60
    {
61 2
        return new self(sprintf('Field "%s" in class "%s" is mapped to field "%s" in the database, but that name is already in use by field "%s".', $offendingFieldName, $document, $databaseName, $originalFieldName));
62
    }
63
64 2
    public static function discriminatorFieldConflict(string $document, string $fieldName) : self
65
    {
66 2
        return new self(sprintf('Discriminator field "%s" in "%s" conflicts with a mapped field\'s "name" attribute.', $fieldName, $document));
67
    }
68
69
    public static function invalidClassInDiscriminatorMap(string $className, string $owningClass) : self
70
    {
71
        return new self(sprintf("Document class '%s' used in the discriminator map of class '%s' does not exist.", $className, $owningClass));
72
    }
73
74 16
    public static function unlistedClassInDiscriminatorMap(string $className) : self
75
    {
76 16
        return new self(sprintf('Document class "%s" is unlisted in the discriminator map.', $className));
77
    }
78
79
    public static function invalidDiscriminatorValue(string $value, string $owningClass) : self
80
    {
81
        return new self(sprintf("Discriminator value '%s' used in the declaration of class '%s' does not exist.", $value, $owningClass));
82
    }
83
84
    public static function missingFieldName(string $className) : self
85
    {
86
        return new self(sprintf("The Document class '%s' field mapping misses the 'fieldName' attribute.", $className));
87
    }
88
89 4
    public static function classIsNotAValidDocument(string $className) : self
90
    {
91 4
        return new self(sprintf('Class %s is not a valid document or mapped super class.', $className));
92
    }
93
94 5
    public static function classCanOnlyBeMappedByOneAbstractDocument(string $className, AbstractDocument $mappedAs, AbstractDocument $offending) : self
95
    {
96 5
        return new self(sprintf(
97 5
            "Can not map class '%s' as %s because it was already mapped as %s.",
98 5
            $className,
99 5
            (new ReflectionObject($offending))->getShortName(),
100 5
            (new ReflectionObject($mappedAs))->getShortName()
101
        ));
102
    }
103
104
    public static function reflectionFailure(string $document, ReflectionException $previousException) : self
105
    {
106
        return new self('An error occurred in ' . $document, 0, $previousException);
107
    }
108
109
    public static function identifierRequired(string $documentName) : self
110
    {
111
        return new self(sprintf("No identifier/primary key specified for Document '%s'. Every Document must have an identifier/primary key.", $documentName));
112
    }
113
114
    public static function missingIdentifierField(string $className, string $fieldName) : self
115
    {
116
        return new self(sprintf('The identifier %s is missing for a query of %s', $fieldName, $className));
117
    }
118
119
    public static function missingIdGeneratorClass(string $className) : self
120
    {
121
        return new self(sprintf('The class-option for the custom ID generator is missing in class %s.', $className));
122
    }
123
124
    public static function classIsNotAValidGenerator(string $className) : self
125
    {
126
        return new self(sprintf('The class %s if not a valid ID generator of type AbstractIdGenerator.', $className));
127
    }
128
129
    public static function missingGeneratorSetter(string $className, string $optionName) : self
130
    {
131
        return new self(sprintf('The class %s is missing a setter for the option %s.', $className, $optionName));
132
    }
133
134 1
    public static function cascadeOnEmbeddedNotAllowed(string $className, string $fieldName) : self
135
    {
136 1
        return new self(sprintf('Cascade on %s::%s is not allowed.', $className, $fieldName));
137
    }
138
139 3
    public static function simpleReferenceRequiresTargetDocument(string $className, string $fieldName) : self
140
    {
141 3
        return new self(sprintf('Target document must be specified for identifier reference: %s::%s', $className, $fieldName));
142
    }
143
144 1
    public static function simpleReferenceMustNotTargetDiscriminatedDocument(string $targetDocument) : self
145
    {
146 1
        return new self(sprintf('Identifier reference must not target document using Single Collection Inheritance, %s targeted.', $targetDocument));
147
    }
148
149 1
    public static function atomicCollectionStrategyNotAllowed(string $strategy, string $className, string $fieldName) : self
150
    {
151 1
        return new self(sprintf('%s collection strategy can be used only in top level document, used in %s::%s', $strategy, $className, $fieldName));
152
    }
153
154 4
    public static function owningAndInverseReferencesRequireTargetDocument(string $className, string $fieldName) : self
155
    {
156 4
        return new self(sprintf('Target document must be specified for owning/inverse sides of reference: %s::%s', $className, $fieldName));
157
    }
158
159 1
    public static function mustNotChangeIdentifierFieldsType(string $className, string $fieldName) : self
160
    {
161 1
        return new self(sprintf('%s::%s was declared an identifier and must stay this way.', $className, $fieldName));
162
    }
163
164 1
    public static function referenceManySortMustNotBeUsedWithNonSetCollectionStrategy(string $className, string $fieldName, string $strategy) : self
165
    {
166 1
        return new self(sprintf("ReferenceMany's sort can not be used with addToSet and pushAll strategies, %s used in %s::%s", $strategy, $className, $fieldName));
167
    }
168
169
    public static function invalidStorageStrategy(string $className, string $fieldName, string $type, string $strategy) : self
170
    {
171
        return new self(sprintf('Invalid strategy %s used in %s::%s with type %s', $strategy, $className, $fieldName, $type));
172
    }
173
174 1
    public static function collectionClassDoesNotImplementCommonInterface(string $className, string $fieldName, string $collectionClass) : self
175
    {
176 1
        return new self(sprintf('%s used as custom collection class for %s::%s has to implement %s interface.', $collectionClass, $className, $fieldName, Collection::class));
177
    }
178
179 2
    public static function shardKeyInSingleCollInheritanceSubclass(string $subclassName) : self
180
    {
181 2
        return new self(sprintf('Shard key overriding in subclass is forbidden for single collection inheritance: %s', $subclassName));
182
    }
183
184 2
    public static function embeddedDocumentCantHaveShardKey(string $className) : self
185
    {
186 2
        return new self(sprintf("Embedded document can't have shard key: %s", $className));
187
    }
188
189 1
    public static function onlySetStrategyAllowedInShardKey(string $className, string $fieldName) : self
190
    {
191 1
        return new self(sprintf('Only fields using the SET strategy can be used in the shard key: %s::%s', $className, $fieldName));
192
    }
193
194 3
    public static function noMultiKeyShardKeys(string $className, string $fieldName) : self
195
    {
196 3
        return new self(sprintf('No multikey indexes are allowed in the shard key: %s::%s', $className, $fieldName));
197
    }
198
199
    public static function cannotLookupDbRefReference(string $className, string $fieldName) : self
200
    {
201
        return new self(sprintf("Cannot use reference '%s' in class '%s' for lookup or graphLookup: dbRef references are not supported.", $fieldName, $className));
202
    }
203
204
    public static function repositoryMethodLookupNotAllowed(string $className, string $fieldName) : self
205
    {
206
        return new self(sprintf("Cannot use reference '%s' in class '%s' for lookup or graphLookup. repositoryMethod is not supported in \$lookup and \$graphLookup stages.", $fieldName, $className));
207
    }
208
209 1
    public static function cannotUseShardedCollectionInOutStage(string $className) : self
210
    {
211 1
        return new self(sprintf("Cannot use class '%s' as collection for out stage. Sharded collections are not allowed.", $className));
212
    }
213
214 3
    public static function cannotUseShardedCollectionInLookupStages(string $className) : self
215
    {
216 3
        return new self(sprintf("Cannot use class '%s' as collection for lookup or graphLookup stage. Sharded collections are not allowed.", $className));
217
    }
218
219
    public static function referencePrimersOnlySupportedForInverseReferenceMany(string $className, string $fieldName) : self
220
    {
221
        return new self(sprintf("Cannot use reference priming on '%s' in class '%s'. Reference priming is only supported for inverse references", $fieldName, $className));
222
    }
223
224 1
    public static function connectFromFieldMustReferenceSameDocument(string $fieldName) : self
225
    {
226 1
        return new self(sprintf("Cannot use field '%s' as connectFromField in a \$graphLookup stage. Reference must target the document itself.", $fieldName));
227
    }
228
229 3
    public static function repositoryMethodCanNotBeCombinedWithSkipLimitAndSort(string $className, string $fieldName) : self
230
    {
231 3
        return new self(sprintf("'repositoryMethod' used on '%s' in class '%s' can not be combined with skip, limit or sort.", $fieldName, $className));
232
    }
233
234 2
    public static function xmlMappingFileInvalid(string $filename, string $errorDetails) : self
235
    {
236 2
        return new self(sprintf("The mapping file %s is invalid: \n%s", $filename, $errorDetails));
237
    }
238
239 1
    public static function fieldNotAllowedForGridFS(string $className, string $fieldName) : self
240
    {
241 1
        return new self(sprintf("Field '%s' in class '%s' is not a valid field for GridFS documents. You should move it to an embedded metadata document.", $fieldName, $className));
242
    }
243
244
    public static function discriminatorNotAllowedForGridFS(string $className) : self
245
    {
246
        return new self(sprintf("Class '%s' cannot be discriminated because it is marked as a GridFS file", $className));
247
    }
248
249
    public static function invalidRepositoryClass(string $className, string $repositoryClass, string $expectedRepositoryClass) : self
250
    {
251
        return new self(sprintf(
252
            'Invalid repository class "%s" for mapped class "%s". It must be an instance of "%s".',
253
            $repositoryClass,
254
            $className,
255
            $expectedRepositoryClass
256
        ));
257
    }
258
259
    public static function viewWithoutRootClass(string $className) : self
260
    {
261
        return new self(sprintf('Class "%s" mapped as view without must have a root class.', $className));
262
    }
263
264
    public static function viewRootClassNotFound(string $className, string $rootClass) : self
265
    {
266
        return new self(sprintf('Root class "%s" for view "%s" could not be found.', $rootClass, $className));
267
    }
268
}
269