Passed
Push — master ( af3dcf...3709c1 )
by Petr
02:16
created

Factory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace kalanis\kw_menu\MetaSource;
4
5
6
use kalanis\kw_files\Access\CompositeAdapter;
7
use kalanis\kw_files\FilesException;
8
use kalanis\kw_menu\Interfaces\IMetaFileParser;
9
use kalanis\kw_menu\Interfaces\IMetaSource;
10
use kalanis\kw_menu\Interfaces\IMNTranslations;
11
use kalanis\kw_menu\MenuException;
12
use kalanis\kw_menu\Traits\TLang;
13
use kalanis\kw_paths\PathsException;
14
use kalanis\kw_storage\Interfaces\IStorage;
15
16
17
/**
18
 * Class Factory
19
 * @package kalanis\kw_menu\MetaSource
20
 * Metadata source is parsed via class
21
 */
22
class Factory
23
{
24
    use TLang;
25
26 9
    public function __construct(?IMNTranslations $lang = null)
27
    {
28 9
        $this->setMnLang($lang);
29 9
    }
30
31
    /**
32
     * @param mixed $params
33
     * @param mixed $parser
34
     * @throws FilesException
35
     * @throws MenuException
36
     * @throws PathsException
37
     * @return IMetaSource
38
     */
39 9
    public function getSource($params, $parser = null): IMetaSource
40
    {
41 9
        if (is_object($params)) {
42 5
            if ($params instanceof IMetaSource) {
43 1
                return $params;
44
            }
45 4
            if ($params instanceof CompositeAdapter) {
46 1
                return new Files($params, $this->getParser($parser));
47
            }
48 3
            if ($params instanceof IStorage) {
49 3
                return new Storage($params, $this->getParser($parser));
50
            }
51 6
        } elseif (is_array($params) && isset($params['source'])) {
52 3
            return $this->getSource(
53 3
                $params['source'],
54 3
                isset($params['parser']) ? $params['parser'] : null
55
            );
56 4
        } elseif (is_string($params)) {
57 4
            return new Volume($params, $this->getParser($parser));
58
        }
59 1
        throw new MenuException($this->getMnLang()->mnNoAvailableMetaSource());
60
    }
61
62
    /**
63
     * @param mixed $parser
64
     * @return IMetaFileParser
65
     */
66 7
    protected function getParser($parser): IMetaFileParser
67
    {
68 7
        return is_object($parser) && ($parser instanceof IMetaFileParser) ? $parser : new FileParser();
69
    }
70
}
71