MutexFactory::getMutex()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Mutex Library.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace AMF\Mutex\Factory;
11
12
use AMF\Mutex\MutexInterface;
13
use AMF\Mutex\Adapter\MemcachedMutex;
14
use AMF\Mutex\Adapter\RedisMutex;
15
use AMF\Mutex\Storage\Memcached\MemcachedFactory;
16
use AMF\Mutex\Storage\Redis\RedisFactory;
17
use AMF\Mutex\Storage\StorageFactoryInterface;
18
19
/**
20
 * Factory for retrieving a mutex instance.
21
 *
22
 * @author Amine Fattouch <[email protected]>
23
 */
24
class MutexFactory
25
{
26
    /**
27
     * @var StorageFactory
28
     */
29
    private $storageFactory;
30
31
    /**
32
     * Constructor class.
33
     *
34
     * @param StorageFactory $storageFactory
35
     */
36
    public function __construct(StorageFactoryInterface $storageFactory)
37
    {
38
        $this->storageFactory = $storageFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $storageFactory of type object<AMF\Mutex\Storage\StorageFactoryInterface> is incompatible with the declared type object<AMF\Mutex\Factory\StorageFactory> of property $storageFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
    }
40
41
    /**
42
     * Returns a mutex.
43
     *
44
     * @return MutexInterface
45
     */
46
    public function getMutex()
47
    {
48
        if ($this->storageFactory instanceof MemcachedFactory) {
49
            return new MemcachedMutex($this->storageFactory->create());
50
        } elseif ($this->storageFactory instanceof RedisFactory) {
51
            return new RedisMutex($this->storageFactory->create());
52
        }
53
54
        throw new \RuntimeException(sprintf('Mutex storage of type "%s" is not supported.', gettype($this->storageFactory)));
55
    }
56
}
57