Completed
Pull Request — master (#1722)
by Maciej
42:42 queued 18:31
created

MongoDBException   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 32.56%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 158
ccs 14
cts 43
cp 0.3256
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A invalidFindByCall() 0 4 1
A detachedDocumentCannotBeRemoved() 0 4 1
A invalidDocumentState() 0 4 1
A documentNotMappedToCollection() 0 4 1
A documentManagerClosed() 0 4 1
A findByRequiresParameter() 0 4 1
A unknownDocumentNamespace() 0 4 1
A cannotPersistMappedSuperclass() 0 4 1
A invalidDocumentRepository() 0 4 1
A invalidValueForType() 0 17 4
A shardKeyFieldCannotBeChanged() 0 4 1
A shardKeyFieldMissing() 0 4 1
A failedToEnableSharding() 0 7 1
A failedToEnsureDocumentSharding() 0 7 1
A commitInProgress() 0 4 1
1
<?php
2
3
namespace Doctrine\ODM\MongoDB;
4
5
/**
6
 * Class for all exceptions related to the Doctrine MongoDB ODM
7
 *
8
 * @since       1.0
9
 */
10
class MongoDBException extends \Exception
11
{
12
    /**
13
     * @param string $documentName
14
     * @param string $fieldName
15
     * @param string $method
16
     * @return MongoDBException
17
     */
18
    public static function invalidFindByCall($documentName, $fieldName, $method)
19
    {
20
        return new self(sprintf('Invalid find by call %s::$fieldName (%s)', $documentName, $fieldName, $method));
21
    }
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
     */
52 5
    public static function documentManagerClosed()
53
    {
54 5
        return new self('The DocumentManager is closed.');
55
    }
56
57
    /**
58
     * @param string $methodName
59
     * @return MongoDBException
60
     */
61
    public static function findByRequiresParameter($methodName)
62
    {
63
        return new self("You need to pass a parameter to '".$methodName."'");
64
    }
65
66
    /**
67
     * @param string $documentNamespaceAlias
68
     * @return MongoDBException
69
     */
70
    public static function unknownDocumentNamespace($documentNamespaceAlias)
71
    {
72
        return new self("Unknown Document namespace alias '$documentNamespaceAlias'.");
73
    }
74
75
    /**
76
     * @param string $className
77
     * @return MongoDBException
78
     */
79 1
    public static function cannotPersistMappedSuperclass($className)
80
    {
81 1
        return new self('Cannot persist an embedded document, aggregation result document or mapped superclass ' . $className);
82
    }
83
84
    /**
85
     * @param string $className
86
     * @return MongoDBException
87
     */
88
    public static function invalidDocumentRepository($className)
89
    {
90
        return new self("Invalid repository class '".$className."'. It must be a Doctrine\Common\Persistence\ObjectRepository.");
91
    }
92
93
    /**
94
     * @param string $type
95
     * @param string|array $expected
96
     * @param mixed $got
97
     * @return MongoDBException
98
     */
99 4
    public static function invalidValueForType($type, $expected, $got)
100
    {
101 4
        if (is_array($expected)) {
102 4
            $expected = sprintf('%s or %s',
103 4
                join(', ', array_slice($expected, 0, -1)),
104 4
                end($expected)
105
            );
106
        }
107 4
        if (is_object($got)) {
108 2
            $gotType = get_class($got);
109 2
        } elseif (is_array($got)) {
110
            $gotType = 'array';
111
        } else {
112 2
            $gotType = 'scalar';
113
        }
114 4
        return new self(sprintf('%s type requires value of type %s, %s given', $type, $expected, $gotType));
115
    }
116
117
    /**
118
     * @param string $field
119
     * @param string $className
120
     * @return MongoDBException
121
     */
122
    public static function shardKeyFieldCannotBeChanged($field, $className)
123
    {
124
        return new self(sprintf('Shard key field "%s" in class "%s" cannot be changed.', $field, $className));
125
    }
126
127
    /**
128
     * @param string $field
129
     * @param string $className
130
     * @return MongoDBException
131
     */
132
    public static function shardKeyFieldMissing($field, $className)
133
    {
134
        return new self(sprintf('Shard key field "%s" in class "%s" is missing.', $field, $className));
135
    }
136
137
    /**
138
     * @param string $dbName
139
     * @param string $errorMessage
140
     * @return MongoDBException
141
     */
142
    public static function failedToEnableSharding($dbName, $errorMessage)
143
    {
144
        return new self(sprintf('Failed to enable sharding for database "%s". Error from MongoDB: %s',
145
            $dbName,
146
            $errorMessage
147
        ));
148
    }
149
150
    /**
151
     * @param string $className
152
     * @param string $errorMessage
153
     * @return MongoDBException
154
     */
155
    public static function failedToEnsureDocumentSharding($className, $errorMessage)
156
    {
157
        return new self(sprintf('Failed to ensure sharding for document "%s". Error from MongoDB: %s',
158
            $className,
159
            $errorMessage
160
        ));
161
    }
162
163
    public static function commitInProgress()
164
    {
165
        return new self('There is already a commit operation in progress');
166
    }
167
}
168