MongoDb   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 7
dl 0
loc 30
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C translate() 0 21 13
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Dao\Exception\Translator;
22
23
/**
24
 * Translates MongoDB driver exceptions
25
 *
26
 * @copyright 2015-2018 LibreWorks contributors
27
 * @license   Apache-2.0
28
 */
29
class MongoDb
30
{
31
    /**
32
     * Translates a MongoDB exception.
33
     *
34
     * @param \Exception $e The exception to translate
35
     * @return \Exception The exception to use
36
     */
37 12
    public static function translate(\Exception $e): \Exception
38
    {
39 12
        if ($e instanceof \MongoDB\Driver\Exception\ConnectionException ||
40 12
                $e instanceof \MongoDB\Driver\Exception\ExecutionTimeoutException) {
41 2
            return new \Caridea\Dao\Exception\Unreachable("System unreachable or connection timed out", $e->getCode(), $e);
42 10
        } elseif ($e instanceof \MongoDB\Driver\Exception\InvalidArgumentException ||
43 9
                $e instanceof \MongoDB\Driver\Exception\LogicException ||
44 10
                $e instanceof \MongoDB\Exception\BadMethodCallException) {
45 3
            return new \Caridea\Dao\Exception\Inoperable("Invalid API usage", $e->getCode(), $e);
46 7
        } elseif ($e instanceof \MongoDB\GridFS\Exception\FileNotFoundException ||
47 7
                $e instanceof \MongoDB\GridFS\Exception\CorruptFileException) {
48 2
            return new \Caridea\Dao\Exception\Unretrievable("Data could not be retrieved", $e->getCode(), $e);
49 5
        } elseif ($e instanceof \MongoDB\Driver\Exception\RuntimeException &&
50 5
                ($e->getCode() == 11000 || 'E11000' == substr($e->getMessage(), 0, 6))) {
51 2
            return new \Caridea\Dao\Exception\Duplicative("Unique constraint violation", 409, $e);
52 3
        } elseif ($e instanceof \MongoDB\Driver\Exception\BulkWriteException &&
53 3
                $e->getMessage() == 'Document failed validation') {
54 1
            return new \Caridea\Dao\Exception\Violating("Constraint violation", 422, $e);
55
        }
56 2
        return new \Caridea\Dao\Exception\Generic("Uncategorized database error", 0, $e);
57
    }
58
}
59