Doctrine::translate()   C
last analyzed

Complexity

Conditions 15
Paths 7

Size

Total Lines 25
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 15

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 22
cts 22
cp 1
rs 5.0504
c 0
b 0
f 0
cc 15
eloc 22
nc 7
nop 1
crap 15

How to fix   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
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Dao\Exception\Translator;
22
23
/**
24
 * Translates Doctrine ORM and ODM exceptions
25
 *
26
 * @copyright 2015-2018 LibreWorks contributors
27
 * @license   Apache-2.0
28
 */
29
class Doctrine
30
{
31
    /**
32
     * Translates a Doctrine exception.
33
     *
34
     * @param \Exception $e The exception to translate
35
     * @return \Exception The exception to use
36
     */
37 13
    public static function translate(\Exception $e): \Exception
38
    {
39 13
        if ($e instanceof \Doctrine\DBAL\Exception\ConnectionException) {
40 1
            return new \Caridea\Dao\Exception\Unreachable("System unreachable or connection timed out", $e->getCode(), $e);
41 12
        } elseif ($e instanceof \Doctrine\ORM\EntityNotFoundException ||
42 11
                $e instanceof \Doctrine\ORM\UnexpectedResultException ||
43 10
                $e instanceof \Doctrine\ODM\MongoDB\DocumentNotFoundException ||
44 12
                $e instanceof \Doctrine\ODM\CouchDB\DocumentNotFoundException) {
45 2
            return new \Caridea\Dao\Exception\Unretrievable("Data could not be retrieved", 404, $e);
46 10
        } elseif ($e instanceof \Doctrine\ORM\PessimisticLockException ||
47 9
                $e instanceof \Doctrine\ORM\OptimisticLockException ||
48 10
                $e instanceof \Doctrine\ODM\CouchDB\OptimisticLockException) {
49 2
            return new \Caridea\Dao\Exception\Conflicting("Optimistic or pessimistic concurrency failure", 409, $e);
50 8
        } elseif ($e instanceof \Doctrine\DBAL\Exception\UniqueConstraintViolationException) {
51 1
            return new \Caridea\Dao\Exception\Duplicative("Unique constraint violation", 409, $e);
52 7
        } elseif ($e instanceof \Doctrine\DBAL\Exception\ConstraintViolationException) {
53 1
            return new \Caridea\Dao\Exception\Violating("Constraint violation", 422, $e);
54 6
        } elseif ($e instanceof \Doctrine\ORM\Query\QueryException ||
55 5
                $e instanceof \Doctrine\ORM\Mapping\MappingException ||
56 4
                $e instanceof \Doctrine\DBAL\Exception\SyntaxErrorException ||
57 6
                $e instanceof \Doctrine\Common\Persistence\Mapping\MappingException) {
58 3
            return new \Caridea\Dao\Exception\Inoperable("Invalid API usage", 0, $e);
59
        }
60 3
        return new \Caridea\Dao\Exception\Generic("Uncategorized database error", 0, $e);
61
    }
62
}
63