Completed
Push — fix_travis ( 785058...ce7855 )
by Kamil
02:06
created

MutexFabric::setDefaultLockImplementorName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of ninja-mutex.
4
 *
5
 * (C) Kamil Dziedzic <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace NinjaMutex;
11
12
use NinjaMutex\Lock\LockInterface;
13
14
/**
15
 * Mutex fabric
16
 *
17
 * @author Kamil Dziedzic <[email protected]>
18
 */
19
class MutexFabric
20
{
21
    protected $defaultLockImplementorName;
22
    protected $implementors = array();
23
    protected $mutexes = array();
24
25
    /**
26
     * @param string $lockImplementorName
27
     * @param $lockImplementor
28
     */
29
    public function __construct($lockImplementorName, LockInterface $lockImplementor)
30
    {
31
        $this->registerLockImplementor($lockImplementorName, $lockImplementor);
32
    }
33
34
    /**
35
     *
36
     * @param  string         $name
37
     * @param  LockInterface  $implementor
38
     * @throws MutexException
39
     */
40
    public function registerLockImplementor($name, LockInterface $implementor)
41
    {
42
        if (isset($this->implementors[$name])) {
43
            throw new MutexException(sprintf('Name %s is already used', $name));
44
        }
45
46
        if (null === $this->defaultLockImplementorName) {
47
            $this->defaultLockImplementorName = $name;
48
        }
49
50
        $this->implementors[$name] = $implementor;
51
    }
52
53
    /**
54
     * @param string $registeredLockImplementorName
55
     */
56
    public function setDefaultLockImplementorName($registeredLockImplementorName)
57
    {
58
        $this->defaultLockImplementorName = $registeredLockImplementorName;
59
    }
60
61
    public function getDefaultLockImplementorName()
62
    {
63
        return $this->defaultLockImplementorName;
64
    }
65
66
    /**
67
     * Create and/or get mutex
68
     *
69
     * @param  string $name
70
     * @param  string $registeredLockImplementorName
71
     * @return Mutex
72
     */
73
    public function get($name, $registeredLockImplementorName = null)
74
    {
75
        if (null === $registeredLockImplementorName) {
76
            $registeredLockImplementorName = $this->getDefaultLockImplementorName();
77
        }
78
79
        if (!isset($this->mutexes[$registeredLockImplementorName][$name])) {
80
            $this->createMutex($name, $registeredLockImplementorName);
81
        }
82
83
        return $this->mutexes[$registeredLockImplementorName][$name];
84
    }
85
86
    /**
87
     * @param string $name
88
     * @param string $registeredLockImplementorName
89
     */
90
    protected function createMutex($name, $registeredLockImplementorName)
91
    {
92
        $this->mutexes[$registeredLockImplementorName][$name] = new Mutex($name, $this->implementors[$registeredLockImplementorName]);
93
    }
94
}
95