Completed
Push — fix-db-exception-not-displayin... ( feb406...668afb )
by Alexander
18:34
created

MysqlMutex   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 49
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A acquireLock() 0 10 1
A releaseLock() 0 10 1
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\mutex;
9
10
use yii\base\InvalidConfigException;
11
12
/**
13
 * MysqlMutex implements mutex "lock" mechanism via MySQL locks.
14
 *
15
 * Application configuration example:
16
 *
17
 * ```
18
 * [
19
 *     'components' => [
20
 *         'db' => [
21
 *             'class' => 'yii\db\Connection',
22
 *             'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
23
 *         ]
24
 *         'mutex' => [
25
 *             'class' => 'yii\mutex\MysqlMutex',
26
 *         ],
27
 *     ],
28
 * ]
29
 * ```
30
 *
31
 * @see Mutex
32
 *
33
 * @author resurtm <[email protected]>
34
 * @since 2.0
35
 */
36
class MysqlMutex extends DbMutex
37
{
38
    /**
39
     * Initializes MySQL specific mutex component implementation.
40
     * @throws InvalidConfigException if [[db]] is not MySQL connection.
41
     */
42 2
    public function init()
43
    {
44 2
        parent::init();
45 2
        if ($this->db->driverName !== 'mysql') {
46
            throw new InvalidConfigException('In order to use MysqlMutex connection must be configured to use MySQL database.');
47
        }
48 2
    }
49
50
    /**
51
     * Acquires lock by given name.
52
     * @param string $name of the lock to be acquired.
53
     * @param int $timeout to wait for lock to become released.
54
     * @return bool acquiring result.
55
     * @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock
56
     */
57 2
    protected function acquireLock($name, $timeout = 0)
58
    {
59
        return $this->db->useMaster(function($db) use ($name, $timeout) {
60
            /** @var \yii\db\Connection $db */
61 2
            return (bool) $db->createCommand(
62 2
                'SELECT GET_LOCK(:name, :timeout)',
63 2
                [':name' => $name, ':timeout' => $timeout]
64 2
            )->queryScalar();
65 2
        });
66
    }
67
68
    /**
69
     * Releases lock by given name.
70
     * @param string $name of the lock to be released.
71
     * @return bool release result.
72
     * @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_release-lock
73
     */
74
    protected function releaseLock($name)
75
    {
76 1
        return $this->db->useMaster(function ($db) use ($name) {
77
            /** @var \yii\db\Connection $db */
78 1
            return (bool)$db->createCommand(
79 1
                'SELECT RELEASE_LOCK(:name)',
80 1
                [':name' => $name]
81 1
            )->queryScalar();
82 1
        });
83
    }
84
}
85