1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the LineMob package. |
5
|
|
|
* |
6
|
|
|
* (c) Ishmael Doss <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LineMob\Core\Doctrine; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
15
|
|
|
use LineMob\Core\Command\AbstractCommand; |
16
|
|
|
use LineMob\Core\Command\FallbackCommand; |
17
|
|
|
use LineMob\Core\Input; |
18
|
|
|
use LineMob\Core\RegistryInterface; |
19
|
|
|
use LineMob\Core\SenderHandlerInterface; |
20
|
|
|
use LineMob\Core\Storage\CommandDataInterface; |
21
|
|
|
use LineMob\Core\Storage\CommandInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Ishmael Doss <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class CommandRegistryDecorator implements RegistryInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var RegistryInterface |
30
|
|
|
*/ |
31
|
|
|
private $decoratedRegistry; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ObjectRepository |
35
|
|
|
*/ |
36
|
|
|
private $commandRepository; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ObjectRepository |
40
|
|
|
*/ |
41
|
|
|
private $commandDataRepository; |
42
|
|
|
|
43
|
|
|
public function __construct( |
44
|
|
|
RegistryInterface $decoratedRegistry, |
45
|
|
|
ObjectRepository $commandRepository, |
46
|
|
|
ObjectRepository $commandDataRepository |
47
|
|
|
) { |
48
|
|
|
$this->decoratedRegistry = $decoratedRegistry; |
49
|
|
|
$this->commandRepository = $commandRepository; |
50
|
|
|
$this->commandDataRepository = $commandDataRepository; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function add($commandClass, SenderHandlerInterface $handler, $default = false) |
57
|
|
|
{ |
58
|
|
|
$this->decoratedRegistry->add($commandClass, $handler, $default); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function getCommandList() |
65
|
|
|
{ |
66
|
|
|
return $this->decoratedRegistry->getCommandList(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return CommandInterface[] |
71
|
|
|
*/ |
72
|
|
|
private function findAllModels() |
73
|
|
|
{ |
74
|
|
|
return $this->commandRepository->findBy(['enabled' => true]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $userId |
79
|
|
|
* |
80
|
|
|
* @return null|object|CommandDataInterface |
81
|
|
|
*/ |
82
|
|
|
private function findUser($userId) |
83
|
|
|
{ |
84
|
|
|
return $this->commandDataRepository->findOneBy(['lineUserId' => $userId]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param Input $input |
89
|
|
|
* |
90
|
|
|
* @return CommandInterface|null |
91
|
|
|
*/ |
92
|
|
|
private function filterModel(Input $input) |
93
|
|
|
{ |
94
|
|
|
$models = array_filter( |
95
|
|
|
$this->findAllModels(), |
96
|
|
|
function (CommandInterface $command) use ($input) { |
97
|
|
|
return preg_match(preg_quote($command->getCmd()), $input->text); |
98
|
|
|
} |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
if (empty($models)) { |
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $models[0]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function findCommandWithModel(Input $input) |
109
|
|
|
{ |
110
|
|
|
$model = $this->filterModel($input); |
111
|
|
|
$default = new FallbackCommand(); |
112
|
|
|
|
113
|
|
|
foreach (array_keys($this->getCommandList()) as $command) { |
114
|
|
|
/** @var AbstractCommand $cmd */ |
115
|
|
|
$cmd = new $command([ |
116
|
|
|
'cmd' => $model->getCmd(), |
117
|
|
|
'name' => $model->getName(), |
118
|
|
|
'description' => $model->getDescription(), |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
if ($cmd->getCode() === $model->getCode()) { |
122
|
|
|
$default = $cmd; |
123
|
|
|
break; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $default; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
public function findCommand(Input $input) |
134
|
|
|
{ |
135
|
|
|
$command = $this->findCommandWithModel($input); |
136
|
|
|
|
137
|
|
|
if ($user = $this->findUser($input->userId)) { |
138
|
|
|
if ($activeCmd = $user->getLineActivedCmd()) { |
|
|
|
|
139
|
|
|
|
140
|
|
|
// no side effect input |
141
|
|
|
$copiedInput = new Input(['text' => $activeCmd]); |
142
|
|
|
|
143
|
|
|
// find previous command |
144
|
|
|
$command = $this->findCommand($copiedInput); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $command; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
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.