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

Mutex   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 82
ccs 18
cts 20
cp 0.9
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
acquireLock() 0 1 ?
releaseLock() 0 1 ?
A init() 0 11 3
A acquire() 0 10 3
A release() 0 13 3
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\Component;
11
12
/**
13
 * The Mutex component allows mutual execution of concurrent processes in order to prevent "race conditions".
14
 *
15
 * This is achieved by using a "lock" mechanism. Each possibly concurrent thread cooperates by acquiring
16
 * a lock before accessing the corresponding data.
17
 *
18
 * Usage example:
19
 *
20
 * ```
21
 * if ($mutex->acquire($mutexName)) {
22
 *     // business logic execution
23
 * } else {
24
 *     // execution is blocked!
25
 * }
26
 * ```
27
 *
28
 * This is a base class, which should be extended in order to implement the actual lock mechanism.
29
 *
30
 * @author resurtm <[email protected]>
31
 * @since 2.0
32
 */
33
abstract class Mutex extends Component
34
{
35
    /**
36
     * @var bool whether all locks acquired in this process (i.e. local locks) must be released automatically
37
     * before finishing script execution. Defaults to true. Setting this property to true means that all locks
38
     * acquired in this process must be released (regardless of errors or exceptions).
39
     */
40
    public $autoRelease = true;
41
42
    /**
43
     * @var string[] names of the locks acquired by the current PHP process.
44
     */
45
    private $_locks = [];
46
47
48
    /**
49
     * Initializes the Mutex component.
50
     */
51 34
    public function init()
52
    {
53 34
        if ($this->autoRelease) {
54 34
            $locks = &$this->_locks;
55 34
            register_shutdown_function(function () use (&$locks) {
56
                foreach ($locks as $lock) {
57
                    $this->release($lock);
58
                }
59 34
            });
60
        }
61 34
    }
62
63
    /**
64
     * Acquires a lock by name.
65
     * @param string $name of the lock to be acquired. Must be unique.
66
     * @param int $timeout time (in seconds) to wait for lock to be released. Defaults to zero meaning that method will return
67
     * false immediately in case lock was already acquired.
68
     * @return bool lock acquiring result.
69
     */
70 34
    public function acquire($name, $timeout = 0)
71
    {
72 34
        if (!in_array($name, $this->_locks, true) && $this->acquireLock($name, $timeout)) {
73 34
            $this->_locks[] = $name;
74
75 34
            return true;
76
        }
77
78 22
        return false;
79
    }
80
81
    /**
82
     * Releases acquired lock. This method will return false in case the lock was not found.
83
     * @param string $name of the lock to be released. This lock must already exist.
84
     * @return bool lock release result: false in case named lock was not found..
85
     */
86 33
    public function release($name)
87
    {
88 33
        if ($this->releaseLock($name)) {
89 33
            $index = array_search($name, $this->_locks);
90 33
            if ($index !== false) {
91 33
                unset($this->_locks[$index]);
92
            }
93
94 33
            return true;
95
        }
96
97 21
        return false;
98
    }
99
100
    /**
101
     * This method should be extended by a concrete Mutex implementations. Acquires lock by name.
102
     * @param string $name of the lock to be acquired.
103
     * @param int $timeout time (in seconds) to wait for the lock to be released.
104
     * @return bool acquiring result.
105
     */
106
    abstract protected function acquireLock($name, $timeout = 0);
107
108
    /**
109
     * This method should be extended by a concrete Mutex implementations. Releases lock by given name.
110
     * @param string $name of the lock to be released.
111
     * @return bool release result.
112
     */
113
    abstract protected function releaseLock($name);
114
}
115