Passed
Push — master ( 2b2b59...ef2215 )
by Wilmer
04:20
created

Transaction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 11
dl 0
loc 30
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A setTransactionIsolationLevel() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite;
6
7
use Throwable;
8
use Yiisoft\Db\Driver\PDO\AbstractTransactionPDO;
9
use Yiisoft\Db\Exception\Exception;
10
use Yiisoft\Db\Exception\InvalidConfigException;
11
use Yiisoft\Db\Exception\NotSupportedException;
12
13
/**
14
 * Implements the SQLite Server specific transaction.
15
 */
16
final class Transaction extends AbstractTransactionPDO
17
{
18
    /**
19
     * Sets the isolation level of the current transaction.
20
     *
21
     * @param string $level The transaction isolation level to use for this transaction.
22
     *
23
     * @see \Yiisoft\Db\Transaction\TransactionInterface::READ_UNCOMMITTED
24
     * @see \Yiisoft\Db\Transaction\TransactionInterface::SERIALIZABLE
25
     *
26
     * @throws Exception
27
     * @throws InvalidConfigException
28
     * @throws NotSupportedException
29
     * @throws Throwable When unsupported isolation levels are used. SQLite only supports `SERIALIZABLE`
30
     * and `READ UNCOMMITTED`.
31
     *
32
     * @link http://www.sqlite.org/pragma.html#pragma_read_uncommitted
33
     */
34 4
    protected function setTransactionIsolationLevel(string $level): void
35
    {
36
        switch ($level) {
37 4
            case self::SERIALIZABLE:
38 1
                $this->db->createCommand('PRAGMA read_uncommitted = False;')->execute();
39 1
                break;
40 4
            case self::READ_UNCOMMITTED:
41 3
                $this->db->createCommand('PRAGMA read_uncommitted = True;')->execute();
42 3
                break;
43
            default:
44 1
                throw new NotSupportedException(
45 1
                    self::class . ' only supports transaction isolation levels READ UNCOMMITTED and SERIALIZABLE.'
46 1
                );
47
        }
48
    }
49
}
50