|
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()); |
|
|
|
|
|
|
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
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: