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

DoctrineDBALTransaction   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 121
ccs 20
cts 26
cp 0.7692
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A rollback() 0 15 2
A __construct() 0 4 1
A commit() 0 16 2
A unescapeBinary() 0 14 3
A execute() 0 25 3
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\DoctrineDBAL;
14
15
use function Amp\call;
16
use Amp\Failure;
17
use Amp\Promise;
18
use Amp\Success;
19
use Doctrine\DBAL\Connection;
20
use Psr\Log\LoggerInterface;
21
use ServiceBus\Storage\Common\Transaction;
22
23
/**
24
 * @internal
25
 */
26
final class DoctrineDBALTransaction implements Transaction
27
{
28
    /** @var Connection */
29
    private $connection;
30
31
    /** @var LoggerInterface */
32
    private $logger;
33
34 5
    public function __construct(Connection $connection, LoggerInterface $logger)
35
    {
36 5
        $this->connection = $connection;
37 5
        $this->logger     = $logger;
38 5
    }
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     * @psalm-suppress MixedReturnTypeCoercion
44
     */
45 5
    public function execute(string $queryString, array $parameters = []): Promise
46
    {
47 5
        $this->logger->debug($queryString, $parameters);
48
49
        try
50
        {
51 5
            $statement = $this->connection->prepare($queryString);
52 5
            $isSuccess = $statement->execute($parameters);
53
54 5
            if ($isSuccess === false)
55
            {
56
                // @codeCoverageIgnoreStart
57
                /** @var string $message Driver-specific error message */
58
                $message = $this->connection->errorInfo()[2];
59
60
                throw new \RuntimeException($message);
61
                // @codeCoverageIgnoreEnd
62
            }
63
64 5
            return new Success(new DoctrineDBALResultSet($this->connection, $statement));
65
        }
66
        // @codeCoverageIgnoreStart
67
        catch (\Throwable $throwable)
68
        {
69
            return new Failure(adaptDbalThrowable($throwable));
70
        }
71
        // @codeCoverageIgnoreEnd
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     *
77
     * @psalm-suppress MixedReturnTypeCoercion
78
     * @psalm-suppress InvalidReturnType
79
     */
80 3
    public function commit(): Promise
81
    {
82
        /** @psalm-suppress InvalidReturnStatement */
83 3
        return call(
84
            function (): void
85
            {
86
                try
87
                {
88 3
                    $this->logger->debug('COMMIT');
89
90 3
                    $this->connection->commit();
91
                }
92
                // @codeCoverageIgnoreStart
93
                catch (\Throwable $throwable)
94
                {
95
                    throw adaptDbalThrowable($throwable);
96
                }
97
                // @codeCoverageIgnoreEnd
98 3
            }
99
        );
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     *
105
     * @psalm-suppress MixedReturnTypeCoercion
106
     * @psalm-suppress InvalidReturnType
107
     */
108 2
    public function rollback(): Promise
109
    {
110
        /** @psalm-suppress InvalidReturnStatement */
111 2
        return call(
112
            function (): void
113
            {
114
                try
115
                {
116 2
                    $this->logger->debug('ROLLBACK');
117
118 2
                    $this->connection->rollBack();
119
                }
120
                // @codeCoverageIgnoreStart
121
                catch (\Throwable $throwable)
122
                {
123
                    /** We will not throw an exception */
124
                }
125
                // @codeCoverageIgnoreEnd
126 2
            }
127
        );
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function unescapeBinary($payload): string
134
    {
135
        /** @var resource|string $payload */
136
        if (\is_resource($payload) === true)
137
        {
138
            $result = \stream_get_contents($payload, -1, 0);
139
140
            if (false !== $result)
141
            {
142
                return $result;
143
            }
144
        }
145
146
        return (string) $payload;
147
    }
148
}
149