1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the PierstovalCharacterManagerBundle package. |
7
|
|
|
* |
8
|
|
|
* (c) Alexandre Rock Ancelet <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Pierstoval\Bundle\CharacterManagerBundle\Resolver; |
15
|
|
|
|
16
|
|
|
use Pierstoval\Bundle\CharacterManagerBundle\Exception\StepNotFoundException; |
17
|
|
|
use Pierstoval\Bundle\CharacterManagerBundle\Model\Step; |
18
|
|
|
use Pierstoval\Bundle\CharacterManagerBundle\Model\StepInterface; |
19
|
|
|
|
20
|
|
|
class StepResolver implements StepResolverInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array[] |
24
|
|
|
*/ |
25
|
|
|
private $managersConfiguration; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var StepInterface[][] |
29
|
|
|
*/ |
30
|
|
|
private $steps; |
31
|
|
|
|
32
|
|
|
public function __construct(array $managersConfiguration = []) |
33
|
|
|
{ |
34
|
|
|
$this->managersConfiguration = $managersConfiguration; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function resolve(string $stepName, string $managerName = null): StepInterface |
38
|
|
|
{ |
39
|
|
|
$this->resolveManagerSteps($managerName); |
40
|
|
|
|
41
|
|
|
$managerName = $this->resolveManagerName($managerName); |
42
|
|
|
|
43
|
|
|
if (!isset($this->steps[$managerName][$stepName])) { |
44
|
|
|
throw new StepNotFoundException($stepName, $managerName); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->steps[$managerName][$stepName]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getManagerSteps(string $managerName = null): array |
51
|
|
|
{ |
52
|
|
|
$this->resolveManagerSteps($managerName); |
53
|
|
|
|
54
|
|
|
return $this->steps[$managerName]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function resolveNumber(int $stepNumber, string $managerName = null): StepInterface |
58
|
|
|
{ |
59
|
|
|
$managerName = $this->resolveManagerName($managerName); |
60
|
|
|
|
61
|
|
|
$this->resolveManagerSteps($managerName); |
62
|
|
|
|
63
|
|
|
foreach ($this->steps[$managerName] as $step) { |
64
|
|
|
if ($step->getNumber() === $stepNumber) { |
65
|
|
|
return $step; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new StepNotFoundException($stepNumber, $managerName); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function resolveManagerName(string $managerName = null): string |
73
|
|
|
{ |
74
|
|
|
if (0 === \count($this->managersConfiguration)) { |
75
|
|
|
throw new \RuntimeException('No character managers to resolve configuration for.'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (null === $managerName && \count($this->managersConfiguration) > 0) { |
79
|
|
|
if (1 === \count($this->managersConfiguration)) { |
80
|
|
|
return \array_keys($this->managersConfiguration)[0]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
throw new \InvalidArgumentException(\sprintf( |
84
|
|
|
'You did not specify which character manager you want to get the steps from, and you have more than one manager. '. |
85
|
|
|
'Possible choices: %s', |
86
|
|
|
\implode(', ', \array_keys($this->managersConfiguration)) |
87
|
|
|
)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!isset($this->managersConfiguration[$managerName])) { |
91
|
|
|
throw new \InvalidArgumentException("\"{$managerName}\" manager does not exist, or is not initialized yet."); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $managerName; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function resolveManagerSteps(string $managerName = null): void |
98
|
|
|
{ |
99
|
|
|
$managerName = $this->resolveManagerName($managerName); |
100
|
|
|
|
101
|
|
|
if (isset($this->steps[$managerName])) { |
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Transform array of array in array of value objects. |
106
|
|
|
foreach ($this->managersConfiguration as $manager => $config) { |
107
|
|
|
if ($manager !== $managerName) { |
108
|
|
|
continue; |
109
|
|
|
} |
110
|
|
|
$this->steps[$manager] = []; |
111
|
|
|
foreach ($config['steps'] as $key => $stepArray) { |
112
|
|
|
$this->steps[$manager][$key] = Step::createFromData($stepArray); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|