Completed
Pull Request — master (#1803)
by Maciej
18:23 queued 15:20
created

MongoDBException   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 34.09%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 0
dl 0
loc 151
ccs 15
cts 44
cp 0.3409
rs 10
c 0
b 0
f 0

14 Methods

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