1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Thinktomorrow\Chief\Management\Assistants; |
5
|
|
|
|
6
|
|
|
use Thinktomorrow\Chief\Management\Exceptions\MissingAssistant; |
7
|
|
|
|
8
|
|
|
trait AssistedManager |
9
|
|
|
{ |
10
|
|
|
protected $assistants = []; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Check if this manager is assisted by a certain assistant |
14
|
|
|
* |
15
|
|
|
* @param string $assistantKey |
16
|
|
|
* @return bool |
17
|
|
|
*/ |
18
|
105 |
|
public function isAssistedBy(string $assistantKey): bool |
19
|
|
|
{ |
20
|
105 |
|
return !! $this->getAssistantClass($assistantKey); |
21
|
|
|
} |
22
|
|
|
|
23
|
110 |
|
/** |
24
|
|
|
* @param bool $asInstances |
25
|
110 |
|
* @return array |
26
|
|
|
* @throws \Exception |
27
|
110 |
|
*/ |
28
|
84 |
|
public function assistants($asInstances = true): array |
29
|
|
|
{ |
30
|
|
|
$assistants = []; |
31
|
110 |
|
|
32
|
|
|
foreach ($this->assistants as $assistant) { |
33
|
|
|
$assistants[] = $asInstances ? $this->assistant($assistant) : $assistant; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $assistants; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function assistantsAsClassNames() |
40
|
|
|
{ |
41
|
99 |
|
return $this->assistants(false); |
42
|
|
|
} |
43
|
99 |
|
|
44
|
1 |
|
/** |
45
|
|
|
* Instantiate the assistant |
46
|
|
|
* |
47
|
98 |
|
* @param string $assistantKey |
48
|
98 |
|
* @return Assistant |
49
|
|
|
* @throws \Exception |
50
|
98 |
|
*/ |
51
|
|
|
public function assistant(string $assistantKey): Assistant |
52
|
|
|
{ |
53
|
105 |
|
if (! $this->isAssistedBy($assistantKey)) { |
54
|
|
|
throw new MissingAssistant('No assistant [' . $assistantKey . '] registered on manager ' . get_class($this)); |
55
|
105 |
|
} |
56
|
84 |
|
|
57
|
|
|
$instance = app($this->getAssistantClass($assistantKey)); |
58
|
|
|
$instance->manager($this); |
|
|
|
|
59
|
24 |
|
|
60
|
18 |
|
return $instance; |
|
|
|
|
61
|
18 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get assistant class by key or assistant classname |
65
|
6 |
|
* |
66
|
|
|
* @param string $assistantKey |
67
|
|
|
* @return string|null |
68
|
|
|
*/ |
69
|
|
|
private function getAssistantClass(string $assistantKey): ?string |
70
|
|
|
{ |
71
|
|
|
foreach ($this->assistants as $class) { |
72
|
|
|
if ($assistantKey == $class::key()) { |
73
|
|
|
return $class; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Alternatively, check if the passed argument is the assistant class name |
78
|
|
|
if (in_array($assistantKey, $this->assistants)) { |
79
|
|
|
return $assistantKey; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function addAssistant(string $assistantClass) |
86
|
|
|
{ |
87
|
|
|
if(false === array_search($assistantClass, $this->assistants)) { |
88
|
|
|
$this->assistants[] = $assistantClass; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.