Completed
Push — fix_travis ( ce7855...06c29f )
by Kamil
03:05
created

MutexFabric   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 76
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A registerLockImplementor() 0 12 3
A setDefaultLockImplementorName() 0 4 1
A getDefaultLockImplementorName() 0 4 1
A get() 0 12 3
A createMutex() 0 4 1
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 84
    public function __construct($lockImplementorName, LockInterface $lockImplementor)
30
    {
31 84
        $this->registerLockImplementor($lockImplementorName, $lockImplementor);
32 84
    }
33
34
    /**
35
     *
36
     * @param  string         $name
37
     * @param  LockInterface  $implementor
38
     * @throws MutexException
39
     */
40 84
    public function registerLockImplementor($name, LockInterface $implementor)
41
    {
42 84
        if (isset($this->implementors[$name])) {
43 14
            throw new MutexException(sprintf('Name %s is already used', $name));
44
        }
45
46 84
        if (null === $this->defaultLockImplementorName) {
47 84
            $this->defaultLockImplementorName = $name;
48 84
        }
49
50 84
        $this->implementors[$name] = $implementor;
51 84
    }
52
53
    /**
54
     * @param string $registeredLockImplementorName
55
     */
56 14
    public function setDefaultLockImplementorName($registeredLockImplementorName)
57
    {
58 14
        $this->defaultLockImplementorName = $registeredLockImplementorName;
59 14
    }
60
61 56
    public function getDefaultLockImplementorName()
62
    {
63 56
        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 28
    public function get($name, $registeredLockImplementorName = null)
74
    {
75 28
        if (null === $registeredLockImplementorName) {
76 14
            $registeredLockImplementorName = $this->getDefaultLockImplementorName();
77 14
        }
78
79 28
        if (!isset($this->mutexes[$registeredLockImplementorName][$name])) {
80 28
            $this->createMutex($name, $registeredLockImplementorName);
81 28
        }
82
83 28
        return $this->mutexes[$registeredLockImplementorName][$name];
84
    }
85
86
    /**
87
     * @param string $name
88
     * @param string $registeredLockImplementorName
89
     */
90 28
    protected function createMutex($name, $registeredLockImplementorName)
91
    {
92 28
        $this->mutexes[$registeredLockImplementorName][$name] = new Mutex($name, $this->implementors[$registeredLockImplementorName]);
93 28
    }
94
}
95