Completed
Pull Request — master (#1790)
by Andreas
20:58
created

MongoDBException::invalidDocumentRepository()   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 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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 5
     */
52
    public static function documentManagerClosed()
53 5
    {
54
        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 1
     */
70
    public static function cannotPersistMappedSuperclass($className)
71 1
    {
72
        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 4
    {
90
        return new self(sprintf("Invalid repository class '%s'. It must be a %s.", $className, GridFSRepository::class));
91 4
    }
92 4
93 4
    /**
94 4
     * @param string       $type
95 4
     * @param string|array $expected
96
     * @param mixed        $got
97
     * @return MongoDBException
98 4
     */
99 2
    public static function invalidValueForType($type, $expected, $got)
100 2
    {
101
        if (is_array($expected)) {
102
            $expected = sprintf(
103 2
                '%s or %s',
104
                implode(', ', array_slice($expected, 0, -1)),
105 4
                end($expected)
106
            );
107
        }
108
        if (is_object($got)) {
109
            $gotType = get_class($got);
110
        } elseif (is_array($got)) {
111
            $gotType = 'array';
112
        } else {
113
            $gotType = 'scalar';
114
        }
115
        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
    public static function cannotPersistGridFSFile(string $className): self
180
    {
181
        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