Completed
Push — master ( f8f0da...220d87 )
by Razon
02:50
created

DbHelper::transaction()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.9666
cc 2
nc 3
nop 4
crap 2
1
<?php
2
namespace App\Helper;
3
4
use Yii;
5
use yii\db\Connection;
6
7
class DbHelper
8
{
9
    /**
10
     * Commits transaction when successed, otherwise rollbacks transaction and rethrow exception.
11
     * 
12
     * @param \Closure       $callback
13
     * @param array          $parameters
14
     * @param null|onnection $db
0 ignored issues
show
Bug introduced by
The type App\Helper\onnection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
     * @param null|string    $isolationLevel
16
     * 
17
     * @throws \Throwable
18
     */
19 2
    public static function transaction(\Closure $callback, array $parameters = [], ?Connection $db = null, ?string $isolationLevel = null)
20
    {
21 2
        $db = $db ?? Yii::$app->getDb();
22 2
        $transaction = $db->beginTransaction($isolationLevel);
23
        try {
24 2
            call_user_func_array($callback, $parameters);
25
26 1
            $transaction->commit();
27 1
        } catch (\Throwable $e) {
28 1
            Yii::error($e, __METHOD__);
29 1
            $transaction->rollBack();
30 1
            throw $e;
31
        }
32
    }
33
}