Completed
Pull Request — master (#56)
by Andreas
02:55
created

ExceptionConverter::toLegacy()   C

Complexity

Conditions 14
Paths 66

Size

Total Lines 54
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 15.8701

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 54
ccs 26
cts 33
cp 0.7879
rs 6.7343
cc 14
eloc 36
nc 66
nop 2
crap 15.8701

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 26
    public static function toLegacy(Exception\Exception $e, $fallbackClass = 'MongoException')
32
    {
33 26
        $message = $e->getMessage();
34 26
        $code = $e->getCode();
35
36 26
        switch (get_class($e)) {
37
            case Exception\AuthenticationException::class:
38
            case Exception\ConnectionException::class:
39
            case Exception\ConnectionTimeoutException::class:
40
            case Exception\SSLConnectionException::class:
41 6
                $class = 'MongoConnectionException';
42 6
                break;
43
44
            case Exception\BulkWriteException::class:
45
            case Exception\WriteException::class:
46 10
                $writeResult = $e->getWriteResult();
47
48 10
                if ($writeResult) {
49 9
                    $writeError = $writeResult->getWriteErrors()[0];
50
51 9
                    $message = $writeError->getMessage();
52 9
                    $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 10
                    case 11000:
58
                    case 11001:
59 1
                    case 12582:
60 9
                        $class = 'MongoDuplicateKeyException';
61 9
                        break;
62
                    default:
63 1
                        $class = 'MongoCursorException';
64
                }
65 10
                break;
66
67 9
            case Exception\ExecutionTimeoutException::class:
68 1
                $class = 'MongoExecutionTimeoutException';
69 1
                break;
70
71
            default:
72 9
                $class = $fallbackClass;
73
        }
74
75 26
        if (strpos($message, 'No suitable servers found') !== false) {
76 2
            return new \MongoConnectionException($message, $code, $e);
77
        }
78
79 24
        if ($message === "cannot use 'w' > 1 when a host is not replicated") {
80 2
            return new \MongoWriteConcernException($message, $code, $e);
81
        }
82
83 22
        return new $class($message, $code, $e);
84
    }
85
}
86