Completed
Pull Request — master (#1866)
by Andreas
13:50
created

MongoDBException   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 48%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 0
dl 0
loc 114
ccs 24
cts 50
cp 0.48
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A unknownDocumentNamespace() 0 4 1
A detachedDocumentCannotBeRemoved() 0 4 1
A invalidDocumentState() 0 4 1
A documentNotMappedToCollection() 0 4 1
A documentManagerClosed() 0 4 1
A cannotPersistMappedSuperclass() 0 4 1
A invalidDocumentRepository() 0 4 1
A invalidGridFSRepository() 0 4 1
A invalidValueForType() 0 18 4
A shardKeyFieldMissing() 0 4 1
A failedToEnableSharding() 0 8 1
A commitInProgress() 0 4 1
A documentBucketOnlyAvailableForGridFSFiles() 0 4 1
A cannotPersistGridFSFile() 0 4 1
A cannotReadGridFSSourceFile() 0 4 1
A failedToEnsureDocumentSharding() 0 8 1
A shardKeyFieldCannotBeChanged() 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 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
    public static function detachedDocumentCannotBeRemoved(): self
24
    {
25
        return new self('Detached document cannot be removed');
26
    }
27
28
    public static function invalidDocumentState(int $state): self
29
    {
30
        return new self(sprintf('Invalid document state "%s"', $state));
31
    }
32
33
    public static function documentNotMappedToCollection(string $className): self
34
    {
35
        return new self(sprintf('The "%s" document is not mapped to a MongoDB database collection.', $className));
36
    }
37
38 5
    public static function documentManagerClosed(): self
39
    {
40 5
        return new self('The DocumentManager is closed.');
41
    }
42
43
    public static function unknownDocumentNamespace(string $documentNamespaceAlias): self
44
    {
45
        return new self(sprintf("Unknown Document namespace alias '%s'.", $documentNamespaceAlias));
46
    }
47
48 1
    public static function cannotPersistMappedSuperclass(string $className): self
49
    {
50 1
        return new self('Cannot persist an embedded document, aggregation result document or mapped superclass ' . $className);
51
    }
52
53
    public static function invalidDocumentRepository(string $className): self
54
    {
55
        return new self(sprintf("Invalid repository class '%s'. It must be a %s.", $className, ObjectRepository::class));
56
    }
57
58
    public static function invalidGridFSRepository(string $className): self
59
    {
60
        return new self(sprintf("Invalid repository class '%s'. It must be a %s.", $className, GridFSRepository::class));
61
    }
62
63
    /**
64
     * @param string|array $expected
65
     * @param mixed        $got
66
     * @return MongoDBException
67
     */
68 4
    public static function invalidValueForType(string $type, $expected, $got): self
69
    {
70 4
        if (is_array($expected)) {
71 4
            $expected = sprintf(
72 4
                '%s or %s',
73 4
                implode(', ', array_slice($expected, 0, -1)),
74 4
                end($expected)
75
            );
76
        }
77 4
        if (is_object($got)) {
78 2
            $gotType = get_class($got);
79 2
        } elseif (is_array($got)) {
80
            $gotType = 'array';
81
        } else {
82 2
            $gotType = 'scalar';
83
        }
84 4
        return new self(sprintf('%s type requires value of type %s, %s given', $type, $expected, $gotType));
85
    }
86
87 2
    public static function shardKeyFieldCannotBeChanged(string $field, string $className): self
88
    {
89 2
        return new self(sprintf('Shard key field "%s" in class "%s" cannot be changed.', $field, $className));
90
    }
91
92
    public static function shardKeyFieldMissing(string $field, string $className): self
93
    {
94
        return new self(sprintf('Shard key field "%s" in class "%s" is missing.', $field, $className));
95
    }
96
97
    public static function failedToEnableSharding(string $dbName, string $errorMessage): self
98
    {
99
        return new self(sprintf(
100
            'Failed to enable sharding for database "%s". Error from MongoDB: %s',
101
            $dbName,
102
            $errorMessage
103
        ));
104
    }
105
106 1
    public static function failedToEnsureDocumentSharding(string $className, string $errorMessage): self
107
    {
108 1
        return new self(sprintf(
109 1
            'Failed to ensure sharding for document "%s". Error from MongoDB: %s',
110 1
            $className,
111 1
            $errorMessage
112
        ));
113
    }
114
115
    public static function commitInProgress(): self
116
    {
117
        return new self('There is already a commit operation in progress. Did you call flush from an event listener?');
118
    }
119
120
    public static function documentBucketOnlyAvailableForGridFSFiles(string $className): self
121
    {
122
        return new self(sprintf('Cannot fetch document bucket for document "%s".', $className));
123
    }
124
125 1
    public static function cannotPersistGridFSFile(string $className): self
126
    {
127 1
        return new self(sprintf('Cannot persist GridFS file for class "%s" through UnitOfWork.', $className));
128
    }
129
130
    public static function cannotReadGridFSSourceFile(string $filename): self
131
    {
132
        return new self(sprintf('Cannot open file "%s" for uploading to GridFS.', $filename));
133
    }
134
}
135