Passed
Push — v4.1 ( 4d3fc3...8dac2f )
by Masiukevich
01:26
created

postgreSqlAdapterFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * SQL databases adapters implementation.
5
 *
6
 * @author  Maksim Masiukevich <[email protected]>
7
 * @license MIT
8
 * @license https://opensource.org/licenses/MIT
9
 */
10
11
declare(strict_types = 1);
12
13
namespace ServiceBus\Storage\Sql\AmpPosgreSQL;
14
15
use Amp\Postgres\QueryExecutionError;
16
use Amp\Sql\ConnectionException;
17
use ServiceBus\Storage\Common\Exceptions as InternalExceptions;
18
use ServiceBus\Storage\Common\StorageConfiguration;
19
20
/**
21
 * Convert AmPHP exceptions.
22
 *
23
 * @internal
24
 *
25
 * @return InternalExceptions\ConnectionFailed|InternalExceptions\StorageInteractingFailed|InternalExceptions\UniqueConstraintViolationCheckFailed
26
 */
27
function adaptAmpThrowable(\Throwable $throwable): \Throwable
28
{
29
    if (
30 4
        $throwable instanceof QueryExecutionError &&
31 4
        \in_array((int) $throwable->getDiagnostics()['sqlstate'], [23503, 23505], true) === true
32
    ) {
33 2
        return new InternalExceptions\UniqueConstraintViolationCheckFailed(
34 2
            $throwable->getMessage(),
35 2
            (int) $throwable->getCode(),
36
            $throwable
37
        );
38
    }
39
40 2
    if ($throwable instanceof ConnectionException)
41
    {
42 1
        return new InternalExceptions\ConnectionFailed(
43 1
            $throwable->getMessage(),
44 1
            (int) $throwable->getCode(),
45
            $throwable
46
        );
47
    }
48
49 1
    return new InternalExceptions\StorageInteractingFailed(
50 1
        $throwable->getMessage(),
51 1
        (int) $throwable->getCode(),
52
        $throwable
53
    );
54
}
55
56
/**
57
 * @internal
58
 *
59
 * @throws InternalExceptions\InvalidConfigurationOptions
60
 */
61
function postgreSqlAdapterFactory(string $connectionDsn): AmpPostgreSQLAdapter
62
{
63 2
    return new AmpPostgreSQLAdapter(
64 2
        new StorageConfiguration($connectionDsn)
65
    );
66
}
67