ExceptionConverter   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 85
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0
wmc 20
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
F toLegacy() 0 61 19
A toResultArray() 0 8 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 */
15
16
namespace Alcaeus\MongoDbAdapter;
17
18
use MongoDB\Driver\Exception;
19
use MongoDB\Driver\WriteError;
20
use MongoDB\Driver\WriteResult;
21
22
/**
23
 * @internal
24
 */
25
class ExceptionConverter
26
{
27
    /**
28
     * @param Exception\Exception $e
29
     * @param string $fallbackClass
30
     *
31
     * @return \MongoException
32
     */
33 33
    public static function toLegacy(Exception\Exception $e, $fallbackClass = 'MongoException')
34
    {
35
        // Starting with ext-mongodb 1.6.0, errors during bulk write are always wrapped in a BulkWriteException.
36
        // If a BulkWriteException wraps another driver exception, use that instead.
37 33
        if ($e instanceof Exception\BulkWriteException && $e->getPrevious() instanceof Exception\Exception) {
0 ignored issues
show
Bug introduced by
The class MongoDB\Driver\Exception\BulkWriteException does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
Bug introduced by
The class MongoDB\Driver\Exception\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
38 2
            $e = $e->getPrevious();
39
        }
40
41 33
        $message = $e->getMessage();
42 33
        $code = $e->getCode();
43
44 33
        switch (get_class($e)) {
45
            case Exception\AuthenticationException::class:
46
            case Exception\ConnectionException::class:
47
            case Exception\ConnectionTimeoutException::class:
48
            case Exception\SSLConnectionException::class:
49 9
                $class = 'MongoConnectionException';
50 9
                break;
51
52
            case Exception\BulkWriteException::class:
53
            case Exception\WriteException::class:
54 12
                $writeResult = $e->getWriteResult();
55
                // attempt to retrieve write error
56 12
                if ($writeResult instanceof WriteResult && is_array($writeResult->getWriteErrors()) && $writeResult->getWriteErrors() !== []) {
0 ignored issues
show
Bug introduced by
The class MongoDB\Driver\WriteResult does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
57 9
                    $writeError = $writeResult->getWriteErrors()[0];
58 9
                    if ($writeError instanceof WriteError) {
0 ignored issues
show
Bug introduced by
The class MongoDB\Driver\WriteError does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
59 9
                        $message = $writeError->getMessage();
60 9
                        $code = $writeError->getCode();
61
                    }
62
                }
63
64
                switch ($code) {
65
                    // see https://github.com/mongodb/mongo-php-driver-legacy/blob/ad3ed45739e9702ae48e53ddfadc482d9c4c7e1c/cursor_shared.c#L540
66 12
                    case 11000:
67 3
                    case 11001:
68 3
                    case 12582:
69 9
                        $class = 'MongoDuplicateKeyException';
70 9
                        break;
71
                    default:
72 3
                        $class = 'MongoCursorException';
73
                }
74 12
                break;
75
76
            case Exception\ExecutionTimeoutException::class:
77 1
                $class = 'MongoExecutionTimeoutException';
78 1
                break;
79
80
            default:
81 11
                $class = $fallbackClass;
82
        }
83
84 33
        if (strpos($message, 'No suitable servers found') !== false) {
85 3
            return new \MongoConnectionException($message, $code, $e);
86
        }
87
88 30
        if ($message === "cannot use 'w' > 1 when a host is not replicated") {
89 2
            return new \MongoWriteConcernException($message, $code, $e);
90
        }
91
92 28
        return new $class($message, $code, $e);
93
    }
94
95
    /**
96
     * Converts an exception to
97
     *
98
     * @param Exception\Exception $e
99
     * @return array
100
     */
101 4
    public static function toResultArray(Exception\Exception $e)
102
    {
103
        return [
104 4
            'ok' => 0.0,
105 4
            'errmsg' => $e->getMessage(),
106 4
            'code' => $e->getCode(),
107
        ];
108
    }
109
}
110