Passed
Pull Request — master (#17)
by
unknown
04:11
created

Factory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace vfalies\tmdb;
4
5
use vfalies\tmdb\Interfaces\Factory\LoggerBuilderInterface;
6
use vfalies\tmdb\Interfaces\Factory\BuilderInterface;
7
use vfalies\tmdb\Exceptions\MissingDependencyException;
8
9
class Factory
10
{
11
    protected $loggerBuilder;
12
13 2
    public static function create($loggerConf = ['builder' => 'NullLogger', 'config' => []])
14
    {
15 2
        $factory = new static();
16 2
        $factory->setLoggerBuilder($factory->getBuilder($loggerConf['builder']), $factory->extractConfig($loggerConf));
0 ignored issues
show
Unused Code introduced by
The call to Factory::setLoggerBuilder() has too many arguments starting with $factory->extractConfig($loggerConf).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
17
18 2
        return $factory;
19
    }
20
21
    /**
22
     *
23
     * @param string $api_key
24
     * @return \vfalies\tmdb\Tmdb
25
     */
26 1
    public function getTmdb(string $api_key)
27
    {
28 1
        return new Tmdb($api_key, $this->loggerBuilder->getLogger());
29
    }
30
31 2
    public function getBuilder($builder, array $args = [])
32
    {
33 2
        $class = "\\vfalies\\tmdb\\Factory\\Builder\\{$builder}Builder";
34
35 2
        $reflection = new \ReflectionClass($class);
36
37 2
        return $reflection->newInstanceArgs($args);
38
    }
39
40 2
    public function setLoggerBuilder(LoggerBuilderInterface $loggerBuilder)
41
    {
42 2
        $this->loggerBuilder = $loggerBuilder;
43
44 2
        return $this;
45
    }
46
47
    /**
48
     * @param $builderConfig
49
     * @return array
50
     */
51 2
    public function extractConfig(array $builderConfig)
52
    {
53 2
        return isset($builderConfig['config']) ? $builderConfig['config']:[];
54
    }
55
56 2
    public function checkDependency(BuilderInterface $builder)
57
    {
58 2
        if (! class_exists($builder->getMainCLassName())) {
59 1
            $message = "missing {$builder->getPackageName()}, please install it using composer : composer require {$builder->getPackageName()}";
60 1
            throw new MissingDependencyException($message);
61
        }
62
63 1
        return true;
64
    }
65
}
66