Completed
Push — master ( 64514c...d88910 )
by Andreas
11s
created

ExceptionConverter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 69.77%

Importance

Changes 0
Metric Value
dl 0
loc 78
ccs 30
cts 43
cp 0.6977
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toResultArray() 0 8 1
C toLegacy() 0 54 14
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
20
/**
21
 * @internal
22
 */
23
class ExceptionConverter
24
{
25
    /**
26
     * @param Exception\Exception $e
27
     * @param string $fallbackClass
28
     *
29
     * @return \MongoException
30
     */
31 13
    public static function toLegacy(Exception\Exception $e, $fallbackClass = 'MongoException')
32
    {
33 13
        $message = $e->getMessage();
34 13
        $code = $e->getCode();
35
36 13
        switch (get_class($e)) {
37 13
            case Exception\AuthenticationException::class:
38 13
            case Exception\ConnectionException::class:
39 13
            case Exception\ConnectionTimeoutException::class:
40 13
            case Exception\SSLConnectionException::class:
41 4
                $class = 'MongoConnectionException';
42 4
                break;
43
44 9
            case Exception\BulkWriteException::class:
45 9
            case Exception\WriteException::class:
46 1
                $writeResult = $e->getWriteResult();
47
48 1
                if ($writeResult) {
49
                    $writeError = $writeResult->getWriteErrors()[0];
50
51
                    $message = $writeError->getMessage();
52
                    $code = $writeError->getCode();
53
                }
54
55
                switch ($code) {
56
                    // see https://github.com/mongodb/mongo-php-driver-legacy/blob/ad3ed45739e9702ae48e53ddfadc482d9c4c7e1c/cursor_shared.c#L540
57 1
                    case 11000:
58 1
                    case 11001:
59 1
                    case 12582:
60
                        $class = 'MongoDuplicateKeyException';
61
                        break;
62 1
                    default:
63 1
                        $class = 'MongoCursorException';
64 1
                }
65 1
                break;
66
67 8
            case Exception\ExecutionTimeoutException::class:
68 1
                $class = 'MongoExecutionTimeoutException';
69 1
                break;
70
71 7
            default:
72 7
                $class = $fallbackClass;
73 13
        }
74
75 13
        if (strpos($message, 'No suitable servers found') !== false) {
76
            return new \MongoConnectionException($message, $code, $e);
77
        }
78
79 13
        if ($message === "cannot use 'w' > 1 when a host is not replicated") {
80
            return new \MongoWriteConcernException($message, $code, $e);
81
        }
82
83 13
        return new $class($message, $code, $e);
84
    }
85
86
    /**
87
     * Converts an exception to
88
     *
89
     * @param Exception\Exception $e
90
     * @return array
91
     */
92
    public static function toResultArray(Exception\Exception $e)
93
    {
94
        return [
95
            'ok' => 0.0,
96
            'errmsg' => $e->getMessage(),
97
            'code' => $e->getCode(),
98
        ];
99
    }
100
}
101