Completed
Push — master ( f3468b...adf0b7 )
by Maciej
11:07 queued 37s
created

MongoDBException::commitInProgress()   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 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
     * @return MongoDBException
14
     */
15
    public static function detachedDocumentCannotBeRemoved()
16
    {
17
        return new self('Detached document cannot be removed');
18
    }
19
20
    /**
21
     * @param string $state
22
     * @return MongoDBException
23
     */
24
    public static function invalidDocumentState($state)
25
    {
26
        return new self(sprintf('Invalid document state "%s"', $state));
27
    }
28
29
    /**
30
     * @param string $className
31
     * @return MongoDBException
32
     */
33
    public static function documentNotMappedToCollection($className)
34
    {
35
        return new self(sprintf('The "%s" document is not mapped to a MongoDB database collection.', $className));
36
    }
37
38
    /**
39
     * @return MongoDBException
40
     */
41 5
    public static function documentManagerClosed()
42
    {
43 5
        return new self('The DocumentManager is closed.');
44
    }
45
46
    /**
47
     * @param string $documentNamespaceAlias
48
     * @return MongoDBException
49
     */
50
    public static function unknownDocumentNamespace($documentNamespaceAlias)
51
    {
52
        return new self("Unknown Document namespace alias '$documentNamespaceAlias'.");
53
    }
54
55
    /**
56
     * @param string $className
57
     * @return MongoDBException
58
     */
59 1
    public static function cannotPersistMappedSuperclass($className)
60
    {
61 1
        return new self('Cannot persist an embedded document, aggregation result document or mapped superclass ' . $className);
62
    }
63
64
    /**
65
     * @param string $className
66
     * @return MongoDBException
67
     */
68
    public static function invalidDocumentRepository($className)
69
    {
70
        return new self("Invalid repository class '".$className."'. It must be a Doctrine\Common\Persistence\ObjectRepository.");
71
    }
72
73
    /**
74
     * @param string $type
75
     * @param string|array $expected
76
     * @param mixed $got
77
     * @return MongoDBException
78
     */
79 4
    public static function invalidValueForType($type, $expected, $got)
80
    {
81 4
        if (is_array($expected)) {
82 4
            $expected = sprintf('%s or %s',
83 4
                join(', ', array_slice($expected, 0, -1)),
84 4
                end($expected)
85
            );
86
        }
87 4
        if (is_object($got)) {
88 2
            $gotType = get_class($got);
89 2
        } elseif (is_array($got)) {
90
            $gotType = 'array';
91
        } else {
92 2
            $gotType = 'scalar';
93
        }
94 4
        return new self(sprintf('%s type requires value of type %s, %s given', $type, $expected, $gotType));
95
    }
96
97
    /**
98
     * @param string $field
99
     * @param string $className
100
     * @return MongoDBException
101
     */
102
    public static function shardKeyFieldCannotBeChanged($field, $className)
103
    {
104
        return new self(sprintf('Shard key field "%s" in class "%s" cannot be changed.', $field, $className));
105
    }
106
107
    /**
108
     * @param string $field
109
     * @param string $className
110
     * @return MongoDBException
111
     */
112
    public static function shardKeyFieldMissing($field, $className)
113
    {
114
        return new self(sprintf('Shard key field "%s" in class "%s" is missing.', $field, $className));
115
    }
116
117
    /**
118
     * @param string $dbName
119
     * @param string $errorMessage
120
     * @return MongoDBException
121
     */
122
    public static function failedToEnableSharding($dbName, $errorMessage)
123
    {
124
        return new self(sprintf('Failed to enable sharding for database "%s". Error from MongoDB: %s',
125
            $dbName,
126
            $errorMessage
127
        ));
128
    }
129
130
    /**
131
     * @param string $className
132
     * @param string $errorMessage
133
     * @return MongoDBException
134
     */
135
    public static function failedToEnsureDocumentSharding($className, $errorMessage)
136
    {
137
        return new self(sprintf('Failed to ensure sharding for document "%s". Error from MongoDB: %s',
138
            $className,
139
            $errorMessage
140
        ));
141
    }
142
143
    /**
144
     * @return MongoDBException
145
     */
146
    public static function commitInProgress()
147
    {
148
        return new self('There is already a commit operation in progress. Did you call flush from an event listener?');
149
    }
150
}
151