Passed
Push — fix-tests ( 6dd538...727178 )
by Alexander
195:41 queued 192:18
created

PgsqlMutex   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 64
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A releaseLock() 0 9 1
A acquireLock() 0 11 1
A init() 0 5 2
A getKeysFromName() 0 3 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\InvalidArgumentException;
11
use yii\base\InvalidConfigException;
12
13
/**
14
 * PgsqlMutex implements mutex "lock" mechanism via PgSQL locks.
15
 *
16
 * Application configuration example:
17
 *
18
 * ```
19
 * [
20
 *     'components' => [
21
 *         'db' => [
22
 *             'class' => 'yii\db\Connection',
23
 *             'dsn' => 'pgsql:host=127.0.0.1;dbname=demo',
24
 *         ]
25
 *         'mutex' => [
26
 *             'class' => 'yii\mutex\PgsqlMutex',
27
 *         ],
28
 *     ],
29
 * ]
30
 * ```
31
 *
32
 * @see Mutex
33
 *
34
 * @author nineinchnick <[email protected]>
35
 * @since 2.0.8
36
 */
37
class PgsqlMutex extends DbMutex
38
{
39
    use RetryAcquireTrait;
40
41
42
    /**
43
     * Initializes PgSQL specific mutex component implementation.
44
     * @throws InvalidConfigException if [[db]] is not PgSQL connection.
45
     */
46 10
    public function init()
47
    {
48 10
        parent::init();
49 10
        if ($this->db->driverName !== 'pgsql') {
50
            throw new InvalidConfigException('In order to use PgsqlMutex connection must be configured to use PgSQL database.');
51
        }
52 10
    }
53
54
    /**
55
     * Converts a string into two 16 bit integer keys using the SHA1 hash function.
56
     * @param string $name
57
     * @return array contains two 16 bit integer keys
58
     */
59 10
    private function getKeysFromName($name)
60
    {
61 10
        return array_values(unpack('n2', sha1($name, true)));
0 ignored issues
show
Bug introduced by
It seems like unpack('n2', sha1($name, true)) can also be of type false; however, parameter $input of array_values() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        return array_values(/** @scrutinizer ignore-type */ unpack('n2', sha1($name, true)));
Loading history...
62
    }
63
64
    /**
65
     * Acquires lock by given name.
66
     * @param string $name of the lock to be acquired.
67
     * @param int $timeout time (in seconds) to wait for lock to become released.
68
     * @return bool acquiring result.
69
     * @see http://www.postgresql.org/docs/9.0/static/functions-admin.html
70
     */
71 10
    protected function acquireLock($name, $timeout = 0)
72
    {
73 10
        list($key1, $key2) = $this->getKeysFromName($name);
74
75
        return $this->retryAcquire($timeout, function () use ($key1, $key2) {
76
            return $this->db->useMaster(function ($db) use ($key1, $key2) {
77
                /** @var \yii\db\Connection $db */
78 10
                return (bool) $db->createCommand(
79 10
                    'SELECT pg_try_advisory_lock(:key1, :key2)',
80 10
                    [':key1' => $key1, ':key2' => $key2]
81 10
                )->queryScalar();
82 10
            });
83 10
        });
84
    }
85
86
    /**
87
     * Releases lock by given name.
88
     * @param string $name of the lock to be released.
89
     * @return bool release result.
90
     * @see http://www.postgresql.org/docs/9.0/static/functions-admin.html
91
     */
92 10
    protected function releaseLock($name)
93
    {
94 10
        list($key1, $key2) = $this->getKeysFromName($name);
95 10
        return $this->db->useMaster(function ($db) use ($key1, $key2) {
96
            /** @var \yii\db\Connection $db */
97 10
            return (bool) $db->createCommand(
98 10
                'SELECT pg_advisory_unlock(:key1, :key2)',
99 10
                [':key1' => $key1, ':key2' => $key2]
100 10
            )->queryScalar();
101 10
        });
102
    }
103
}
104