Completed
Push — master ( 7db4b5...da5074 )
by Andreas
10s
created

MongoDBException::invalidTypeMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 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 Exception;
10
use function array_slice;
11
use function end;
12
use function get_class;
13
use function implode;
14
use function is_array;
15
use function is_object;
16
use function sprintf;
17
18
/**
19
 * Class for all exceptions related to the Doctrine MongoDB ODM
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
     *
67
     * @return MongoDBException
68
     */
69 4
    public static function invalidValueForType(string $type, $expected, $got) : self
70
    {
71 4
        if (is_array($expected)) {
72 4
            $expected = sprintf(
73 4
                '%s or %s',
74 4
                implode(', ', array_slice($expected, 0, -1)),
75 4
                end($expected)
76
            );
77
        }
78 4
        if (is_object($got)) {
79 2
            $gotType = get_class($got);
80 2
        } elseif (is_array($got)) {
81
            $gotType = 'array';
82
        } else {
83 2
            $gotType = 'scalar';
84
        }
85 4
        return new self(sprintf('%s type requires value of type %s, %s given', $type, $expected, $gotType));
86
    }
87
88 2
    public static function shardKeyFieldCannotBeChanged(string $field, string $className) : self
89
    {
90 2
        return new self(sprintf('Shard key field "%s" in class "%s" cannot be changed.', $field, $className));
91
    }
92
93
    public static function shardKeyFieldMissing(string $field, string $className) : self
94
    {
95
        return new self(sprintf('Shard key field "%s" in class "%s" is missing.', $field, $className));
96
    }
97
98
    public static function failedToEnableSharding(string $dbName, string $errorMessage) : self
99
    {
100
        return new self(sprintf(
101
            'Failed to enable sharding for database "%s". Error from MongoDB: %s',
102
            $dbName,
103
            $errorMessage
104
        ));
105
    }
106
107 1
    public static function failedToEnsureDocumentSharding(string $className, string $errorMessage) : self
108
    {
109 1
        return new self(sprintf(
110 1
            'Failed to ensure sharding for document "%s". Error from MongoDB: %s',
111 1
            $className,
112 1
            $errorMessage
113
        ));
114
    }
115
116
    public static function commitInProgress() : self
117
    {
118
        return new self('There is already a commit operation in progress. Did you call flush from an event listener?');
119
    }
120
121
    public static function documentBucketOnlyAvailableForGridFSFiles(string $className) : self
122
    {
123
        return new self(sprintf('Cannot fetch document bucket for document "%s".', $className));
124
    }
125
126 1
    public static function cannotPersistGridFSFile(string $className) : self
127
    {
128 1
        return new self(sprintf('Cannot persist GridFS file for class "%s" through UnitOfWork.', $className));
129
    }
130
131
    public static function cannotReadGridFSSourceFile(string $filename) : self
132
    {
133
        return new self(sprintf('Cannot open file "%s" for uploading to GridFS.', $filename));
134
    }
135
136 3
    public static function invalidTypeMap(string $part, string $epectedType) : self
137
    {
138 3
        return new self(sprintf('Invalid typemap provided. Type "%s" is required for "%s".', $epectedType, $part));
139
    }
140
}
141