Completed
Pull Request — master (#20)
by
unknown
02:34
created

ExceptionConverter::convertException()   D

Complexity

Conditions 9
Paths 16

Size

Total Lines 29
Code Lines 20

Duplication

Lines 3
Ratio 10.34 %

Code Coverage

Tests 12
CRAP Score 13.0498

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 3
loc 29
ccs 12
cts 19
cp 0.6316
rs 4.909
cc 9
eloc 20
nc 16
nop 1
crap 13.0498
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
     * @return \MongoException
27
     */
28 21
    public static function convertException(Exception\Exception $e)
29
    {
30 21
        switch (get_class($e)) {
31
            case Exception\AuthenticationException::class:
32
            case Exception\ConnectionException::class:
33
            case Exception\ConnectionTimeoutException::class:
34
            case Exception\SSLConnectionException::class:
35 8
                $class = 'MongoConnectionException';
36 8
                break;
37
38
            case Exception\BulkWriteException::class:
39
            case Exception\WriteException::class:
40 1
                $class = 'MongoCursorException';
41 1
                break;
42
43 11
            case Exception\ExecutionTimeoutException::class:
44 1
                $class = 'MongoExecutionTimeoutException';
45 1
                break;
46
47
            default:
48 11
                $class = 'MongoException';
49
        }
50
51 21 View Code Duplication
        if (strpos($e->getMessage(), 'No suitable servers found') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
            return new \MongoConnectionException($e->getMessage(), $e->getCode(), $e);
53
        }
54
55 21
        return new $class($e->getMessage(), $e->getCode(), $e);
56
    }
57
58
    /**
59
     * @throws \MongoException
60
     */
61 7
    public static function toLegacy(Exception\Exception $e)
62
    {
63 7
        self::isNoServerAvailable($e);
64 5
        throw self::convertException($e);
65
    }
66
67 7
    public static function isNoServerAvailable(Exception\Exception $e)
68
    {
69 7 View Code Duplication
        if (strpos($e->getMessage(), 'No suitable servers found') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70 2
            throw new \MongoConnectionException($e->getMessage(), $e->getCode(), $e);
71
        }
72 5
    }
73
}
74