Factory::getSemaphore()   C
last analyzed

Complexity

Conditions 15
Paths 14

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 15

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 15
eloc 23
c 1
b 0
f 1
nc 14
nop 1
dl 0
loc 37
ccs 25
cts 25
cp 1
crap 15
rs 5.9166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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()->smCannotGetSemaphoreClass());
70
    }
71
}
72