GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( bbf2ff...d7ffda )
by Robert
12:47
created

PgsqlMutex   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 66
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A getKeysFromName() 0 4 1
A acquireLock() 0 14 1
A releaseLock() 0 11 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
     * Initializes PgSQL specific mutex component implementation.
43
     * @throws InvalidConfigException if [[db]] is not PgSQL connection.
44
     */
45 10
    public function init()
46
    {
47 10
        parent::init();
48 10
        if ($this->db->driverName !== 'pgsql') {
49
            throw new InvalidConfigException('In order to use PgsqlMutex connection must be configured to use PgSQL database.');
50
        }
51 10
    }
52
53
    /**
54
     * Converts a string into two 16 bit integer keys using the SHA1 hash function.
55
     * @param string $name
56
     * @return array contains two 16 bit integer keys
57
     */
58 10
    private function getKeysFromName($name)
59
    {
60 10
        return array_values(unpack('n2', sha1($name, true)));
61
    }
62
63
    /**
64
     * Acquires lock by given name.
65
     * @param string $name of the lock to be acquired.
66
     * @param int $timeout time (in seconds) to wait for lock to become released.
67
     * @return bool acquiring result.
68
     * @see http://www.postgresql.org/docs/9.0/static/functions-admin.html
69
     */
70 10
    protected function acquireLock($name, $timeout = 0)
71
    {
72 10
        list($key1, $key2) = $this->getKeysFromName($name);
73
74
        return $this->retryAcquire($timeout, function () use ($key1, $key2) {
75
            return $this->db->useMaster(function ($db) use ($key1, $key2) {
76
                /** @var \yii\db\Connection $db */
77 10
                return (bool) $db->createCommand(
78 10
                    'SELECT pg_try_advisory_lock(:key1, :key2)',
79 10
                    [':key1' => $key1, ':key2' => $key2]
80 10
                )->queryScalar();
81 10
            });
82 10
        });
83
    }
84
85
    /**
86
     * Releases lock by given name.
87
     * @param string $name of the lock to be released.
88
     * @return bool release result.
89
     * @see http://www.postgresql.org/docs/9.0/static/functions-admin.html
90
     */
91 10
    protected function releaseLock($name)
92
    {
93 10
        list($key1, $key2) = $this->getKeysFromName($name);
94 10
        return $this->db->useMaster(function ($db) use ($key1, $key2) {
95
            /** @var \yii\db\Connection $db */
96 10
            return (bool) $db->createCommand(
97 10
                'SELECT pg_advisory_unlock(:key1, :key2)',
98 10
                [':key1' => $key1, ':key2' => $key2]
99 10
            )->queryScalar();
100 10
        });
101
    }
102
}
103