Completed
Push — v4.1 ( f3a0c7...588d2a )
by Masiukevich
07:33
created

AmpPostgreSQLTransaction::commit()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 20
ccs 4
cts 4
cp 1
crap 2
rs 9.9666
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\PgSqlCommandResult;
16
use Amp\Postgres\PooledResultSet;
17
use Amp\Postgres\PqCommandResult;
18
use Amp\Sql\ResultSet as AmpResultSet;
19
use function Amp\call;
20
use Amp\Postgres\Transaction as AmpTransaction;
21
use Amp\Promise;
22
use Psr\Log\LoggerInterface;
23
use ServiceBus\Storage\Common\Transaction;
24
25
/**
26
 * Async PostgreSQL transaction adapter.
27
 *
28
 * @internal
29
 */
30
final class AmpPostgreSQLTransaction implements Transaction
31
{
32
    /** @var AmpTransaction */
33
    private $transaction;
34
35
    /** @var LoggerInterface */
36
    private $logger;
37
38 5
    public function __construct(AmpTransaction $transaction, LoggerInterface $logger)
39
    {
40 5
        $this->transaction = $transaction;
41 5
        $this->logger      = $logger;
42 5
    }
43
44 5
    public function __destruct()
45
    {
46 5
        if ($this->transaction->isAlive() === true)
47
        {
48
            $this->transaction->close();
49
        }
50 5
    }
51
52
    /**
53
     * {@inheritdoc}
54
     *
55
     * @psalm-suppress MixedReturnTypeCoercion
56
     */
57 5
    public function execute(string $queryString, array $parameters = []): Promise
58
    {
59 5
        return call(
60
            function () use ($queryString, $parameters): \Generator
61
            {
62
                try
63
                {
64 5
                    $this->logger->debug($queryString, $parameters);
65
66
                    /** @var AmpResultSet|PgSqlCommandResult|PooledResultSet|PqCommandResult $resultSet */
67 5
                    $resultSet = yield $this->transaction->execute($queryString, $parameters);
68
69 5
                    return new AmpPostgreSQLResultSet($resultSet);
70
                }
71
                // @codeCoverageIgnoreStart
72
                catch (\Throwable $throwable)
73
                {
74
                    throw adaptAmpThrowable($throwable);
75
                }
76
                // @codeCoverageIgnoreEnd
77 5
            }
78
        );
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     *
84
     * @psalm-suppress MixedReturnTypeCoercion
85
     */
86 3
    public function commit(): Promise
87
    {
88 3
        return call(
89
            function (): \Generator
90
            {
91
                try
92
                {
93 3
                    $this->logger->debug('COMMIT');
94
95
                    /** @psalm-suppress TooManyTemplateParams */
96 3
                    yield $this->transaction->commit();
97
                }
98
                // @codeCoverageIgnoreStart
99
                catch (\Throwable $throwable)
100
                {
101
                    throw adaptAmpThrowable($throwable);
102
                }
103
                finally
104
                {
105
                    $this->transaction->close();
106
                }
107
                // @codeCoverageIgnoreEnd
108 3
            }
109
        );
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     *
115
     * @psalm-suppress MixedReturnTypeCoercion
116
     */
117 2
    public function rollback(): Promise
118
    {
119 2
        return call(
120
            function (): \Generator
121
            {
122
                try
123
                {
124 2
                    $this->logger->debug('ROLLBACK');
125
126 2
                    yield $this->transaction->rollback();
127
                }
128
                // @codeCoverageIgnoreStart
129
                catch (\Throwable $throwable)
130
                {
131
                    /** We will not throw an exception */
132
                }
133
                finally
134
                {
135
                    $this->transaction->close();
136
                }
137
                // @codeCoverageIgnoreEnd
138 2
            }
139
        );
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function unescapeBinary($payload): string
146
    {
147
        if (\is_resource($payload) === true)
148
        {
149
            $payload = \stream_get_contents($payload, -1, 0);
150
        }
151
152
        return \pg_unescape_bytea((string) $payload);
153
    }
154
}
155