Passed
Push — master ( 0000af...b571e7 )
by Petr
07:35
created

Factory   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 26
c 1
b 0
f 1
dl 0
loc 51
ccs 28
cts 28
cp 1
rs 10
wmc 16

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C getSemaphore() 0 37 15
1
<?php
2
3
namespace kalanis\kw_semaphore\Semaphore;
4
5
6
use kalanis\kw_files\Access\CompositeAdapter;
7
use kalanis\kw_semaphore\Interfaces\ISemaphore;
8
use kalanis\kw_semaphore\Interfaces\ISMTranslations;
9
use kalanis\kw_semaphore\SemaphoreException;
10
use kalanis\kw_semaphore\Traits\TLang;
11
use kalanis\kw_storage\Interfaces\IStorage;
12
13
14
/**
15
 * Class Factory
16
 * @package kalanis\kw_semaphore\Semaphore
17
 * Data source for semaphore is files
18
 */
19
class Factory
20
{
21
    use TLang;
22
23 10
    public function __construct(?ISMTranslations $lang = null)
24
    {
25 10
        $this->setSmLang($lang);
26 10
    }
27
28
    /**
29
     * @param mixed $params
30
     * @throws SemaphoreException
31
     * @return ISemaphore
32
     */
33 10
    public function getSemaphore($params): ISemaphore
34
    {
35 10
        if (is_object($params) && ($params instanceof ISemaphore)) {
36 1
            return $params;
37
        }
38 9
        if (is_array($params)) {
39 7
            if (isset($params['semaphore'])) {
40 7
                if (is_object($params['semaphore'])) {
41 6
                    if ($params['semaphore'] instanceof ISemaphore) {
42 2
                        return $params['semaphore'];
43
                    }
44 4
                    if ($params['semaphore'] instanceof CompositeAdapter) {
45 2
                        return new Files(
46 2
                            $params['semaphore'],
47 2
                            isset($params['semaphore_root']) && is_array($params['semaphore_root'])
48 1
                                ? $params['semaphore_root']
49 2
                                : []
50
                        );
51
                    }
52 2
                    if ($params['semaphore'] instanceof IStorage) {
53 2
                        return new Storage(
54 2
                            $params['semaphore'],
55 2
                            isset($params['semaphore_root']) && is_string($params['semaphore_root'])
56 1
                                ? $params['semaphore_root']
57 2
                                : ''
58
                        );
59
                    }
60
                }
61 1
                if (is_string($params['semaphore'])) {
62 1
                    return new Volume($params['semaphore'], $this->getSmLang());
63
                }
64
            }
65
        }
66 2
        if (is_string($params)) {
67 1
            return new Volume($params, $this->getSmLang());
68
        }
69 1
        throw new SemaphoreException($this->getSmLang()->mnCannotGetSemaphoreClass());
70
    }
71
}
72