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

ExceptionConverter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 51
Duplicated Lines 11.76 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 73.08%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 12
c 2
b 0
f 2
lcom 0
cbo 2
dl 6
loc 51
ccs 19
cts 26
cp 0.7308
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
D convertException() 3 29 9
A toLegacy() 0 5 1
A isNoServerAvailable() 3 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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