Completed
Push — master ( c5047e...3a177f )
by Gabriel
03:52
created

CommandsFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 55
ccs 14
cts 18
cp 0.7778
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 1
A createFromAlias() 0 6 1
A hydrateConfiguration() 0 7 2
A hydrateModelRegistry() 0 7 2
1
<?php
2
3
4
namespace Nip\Records\Locator\Resolver\Commands;
5
6
use Nip\Records\Locator\Configuration\Configuration;
7
use Nip\Records\Locator\ModelLocator;
8
use Nip\Records\Locator\Registry\ModelRegistry;
9
10
/**
11
 * Class CommandsFactory
12
 * @package Nip\Records\Locator\Resolver\Commands
13
 */
14
class CommandsFactory
15
{
16
17
    /**
18
     * @param string $alias
19
     * @param ModelLocator $modelLocator
20
     * @return Command
21
     */
22 6
    public static function create($alias, $modelLocator = null)
23
    {
24 6
        $command = new Command();
25 6
        $command->setAlias($alias);
26 6
        $command = self::hydrateConfiguration($command, $modelLocator->getConfiguration());
0 ignored issues
show
Bug introduced by
It seems like $modelLocator is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
27 6
        $command = self::hydrateModelRegistry($command, $modelLocator->getModelRegistry());
28 6
        return $command;
29
    }
30
31
    /**
32
     * @param string $alias
33
     * @param Configuration|null $configuration
34
     * @return Command
35
     */
36
    public static function createFromAlias($alias, $configuration = null)
37
    {
38
        $command = new Command();
39
        $command->setAlias($alias);
40
        return self::hydrateConfiguration($command, $configuration);
41
    }
42
43
    /**
44
     * @param Command $command
45
     * @param Configuration|null $configuration
46
     * @return Command
47
     */
48 6
    protected static function hydrateConfiguration(Command $command, $configuration)
49
    {
50 6
        if ($configuration instanceof Configuration) {
51 6
            $command->setConfiguration($configuration);
52
        }
53 6
        return $command;
54
    }
55
56
    /**
57
     * @param Command $command
58
     * @param ModelRegistry|null $registry
59
     * @return Command
60
     */
61 6
    protected static function hydrateModelRegistry(Command $command, $registry)
62
    {
63 6
        if ($registry instanceof ModelRegistry) {
64 6
            $command->setModelRegistry($registry);
65
        }
66 6
        return $command;
67
    }
68
}
69